Not quite programming: batch file

yukichigai

Diamond Member
Apr 23, 2003
6,404
0
76
Short and sweet, I need a batch file to do something kind of weird. I need it to go through a folder (and its subdirectories), and for each file of extension ".foo" I need it to place a copy of a separate file in the folder with the same name as the first file, but with the extension ".fop". To put it into an example, say I have a folder with these files:

thing.foo
whatever.foo
yup.foo

I want the batch file to look at those files, then copy ANOTHER file into that folder and rename it for each file, so I wind up with:

thing.foo
thing.fop
whatever.foo
whatever.fop
yup.foo
yup.fop

All the ".fop" files being copies of one specific file.

Any way of doing this in a batch? I've sort of tried with what batch syntax I know, but I'm getting nowhere.
 

Patterner

Senior member
Dec 20, 2010
227
0
0
Not sure if there's a way to do this with batch (it's been awhile since I've done any major batch file stuff), but it's a pretty easy powershell script. You'll want to install powershell (if you're running Win7 it should already be installed) and save this as a .ps1 file and run it from the directory in question.

Code:
$copyFile = Get-ChildItem "c:\path to file you want to copy.whatevs"
$files = Get-ChildItem
foreach ($file in $files)
{
	if ($file.extension -eq ".foo")
	{
		$newName = $file.name.replace(".foo", ".fop")
		$copyFile.copyTo($file.DirectoryName + "\" + $newName)
	}
}
 

Patterner

Senior member
Dec 20, 2010
227
0
0
Doesn't even require a script.

Code:
copy *.foo *.fop

Except the OP's wanting to copy another file from a different location into the folder with the .foo files and give that newly copied file the extension of .fop.

I'm presuming that there are other files in the folder than those with the .foo extension.

At least, that's how I read it.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Except the OP's wanting to copy another file from a different location into the folder with the .foo files and give that newly copied file the extension of .fop.

I'm presuming that there are other files in the folder than those with the .foo extension.

At least, that's how I read it.

The command above ignores all files with extensions other than .foo.... That is how the wildcard works.
 

Patterner

Senior member
Dec 20, 2010
227
0
0
The command above ignores all files with extensions other than .foo.... That is how the wildcard works.

I realise that, I was commenting on the conditional logic in my script. Since it would be easier without it (I could do it as a one liner).
 
Last edited:

MerlinRML

Senior member
Sep 9, 2005
207
0
71
Cogman's solution might be on the right track, but it's lacking the directory traversal piece.

Use a for loop with the 'dir /s/b /a:d' command to get the dirs and subdirs then run the copy path\*.foo to path\*.fop and you're good to go.

I'd swear there was a better way to do the directory traversal instead of having a loop (something like copy -R if it existed), but I'm drawing a blank on that at the moment.

*EDIT*
As I reread the problem, it occurs to me that the "ANOTHER file", as posted by the OP, may be a file stored off somewhere in the ether, in which case I'd modify my solution to use a for loop that uses 'dir /s/b *.foo' and then run copy source_path\source.file path\variable.fop

Either way, the code is pretty similar.
 
Last edited:

sao123

Lifer
May 27, 2002
12,653
205
106
here is something I wrote a long time ago to rename an entire directory of files...
all of the echo stuff was to keep everything into one file, it self expands then self deletes.
The first part does a dir into a text file, then it parses and performs the actions on the files one at a time.

The bolded portion of the file is where you would need to modify to make the system perform your necessary steps... (COPY then RENAME).

Hope this helps a bit.

DECOMPRESS.BAT

echo>main.bat dir /b *.tif ^> files.txt
echo>>main.bat echo -1 ^>^> files.txt
echo>>main.bat set /a cnt=1
echo>>main.bat :Start
echo>>main.bat copy fragment.dll + files.txt data.bat ^> NUL
echo>>main.bat call data.bat
echo>>main.bat set /a cnt = %%cnt%% + 1
echo>>main.bat type data.bat ^| find /v "process.bat" ^> files.txt
echo>>main.bat findstr /o /c:"-1" files.txt ^| findstr /b /c:"0:-1" ^> NUL
echo>>main.bat IF ERRORLEVEL 1 goto Start
echo>>main.bat :End
echo>>main.bat del files.txt
echo>>main.bat del rename.bat
echo>>main.bat del data.bat
echo>process.bat set ful=%%1
echo>>process.bat for /F "delims=\ " %%%%I in ("%%CD%%") do set e=%%%%~nI
echo>>process.bat echo %%e%%hi
echo>>process.bat echo %%cnt%%hi
echo>>process.bat if %%cnt%% GTR 99 goto base
echo>>process.bat if %%cnt%% GTR 9 goto ten
echo>>process.bat echo ren %%ful%% %%e%%00%%cnt%%.tif ^> rename.bat
echo>>process.bat GOTO thend
echo>>process.bat :ten
echo>>process.bat echo ren %%ful%% %%e%%0%%cnt%%.tif ^> rename.bat
echo>>process.bat GOTO thend
echo>>process.bat :base
echo>>process.bat echo ren %%ful%% %%e%%%%cnt%%.tif ^> rename.bat
echo>>process.bat :thend
echo>>process.bat call rename.bat
CALL MAIN.bat
DEL MAIN.BAT
DEL PROCESS.BAT


You will need to create this in addition to the original batch file.

Fragment.bat

Process.bat

The only thing noteworthy of this, is to make sure there is exaclty 1 space after process.bat and no return (nextline).
 
Last edited:

sao123

Lifer
May 27, 2002
12,653
205
106
Ok, I realize the above seems quite complex... so now that ive had time to go back over it... ill explain it to you. The above batch file creates 2 other batch files and then calls them. Here they are, with explanation.

MAIN.bat

dir /b *.tif > files.txt
echo -1 >> files.txt
set /a cnt=1
PAUSE
:Start
copy fragment.bat + files.txt data.bat > NUL
call data.bat
set /a cnt = %cnt% + 1
type data.bat | find /v "process.bat" > files.txt
findstr /o /c:"-1" files.txt | findstr /b /c:"0:-1" > NUL
IF ERRORLEVEL 1 goto Start
:End
del files.txt
del rename.bat
del data.bat



So this is the main file, it takes a directory of the folder its in and stores it in a txt file. I have bolded the first line, because this is one you'll need to change to fit your purposes... I was collecting TIF files, you arent.
The next few lines do some fancy concatenation in order to be able to pass the first filename in the txt file to the processing routine. The below fragment.bat file simply contains the name of the other file process.bat.
So, it copies the text from fragment and the first filename into a new file data.bat. Data.bat will contain something like the following.
Process.bat File1.tif
Then data.bat gets called and ran, which then calls process and passes it File1.tif.




process.bat

set ful=%1
for /F "delims=\ " %%I in ("%CD%") do set e=%%~nI
echo %e%hi
echo %cnt%hi
if %cnt% GTR 99 goto base
if %cnt% GTR 9 goto ten
echo ren %ful% %e%00%cnt%.tif > rename.bat
GOTO thend
:ten
echo ren %ful% %e%0%cnt%.tif > rename.bat
GOTO thend
:base
echo ren %ful% %e%%cnt%.tif > rename.bat
:thend
call rename.bat


So here is where the actual work happens... now ignore all that im doing here, its a log of get the folder name, conditionally rename etc.

%1 is the argument which contains your file name. You will want to copy it into a variable for use, which is what the line set ful=%1 does. %ful% is now your variable.

Then you will only need a simple 3-4 lines here. your goal is to COPY and then RENAME the file. Replace the boldded with your code.
 
Last edited: