Reputation: 95
I have about 10 batch files which copy *.txt files from root folder to their subfolders. The code for copying is following:
cscript ..\myScripts\CopyFiles.vbs "." "..\myScripts\Copy.cfg" "scripts_A" "((.+\.txt)|(.+\.ps1))"
Every single batch file copy *.txt files to folder with the same name (A.bat ->scripts_A, B.bat ->scripts_B). I need to have right filename in each subfolder (in folder scripts_A files need to be operationAfirst.txt, operationAsecond.txt, in folder scripts_B files operationBfirst.txt, operationBsecond.txt). So the result should be following:
for folder scripts_A:
operationAfirst.txt
operationAsecond.txt
for folder scripts_B:
operationBfirst.txt
operationBsecond.txt
All *.txt files in subfolders are usually clones of *.txt files for folder scripts_A. I just need to update their filename. Is there any posibility how to edit batch file, which can replace "A" string in filename for "B" in batch used for folder scripts_B?
Upvotes: 0
Views: 477
Reputation:
Instead of repairing the outcome of a poorly designed setup, I'd prefer to give hints how to alter that situation. So against the best of one's knowledge this batch will do the rename:
@Echo off
Setlocal
CD /D X:\where\ever\you\start
For %%A in (A B C D E F G H I J) Do For /F "delims=" %%B in (
'Dir /B %CD%\Script_%%A\operation*.txt |Findstr /V "operation%%A"'
) Do Call :RenFiles %%A "%%~fB"
Goto :Eof
:RenFiles
Set OldName=%~nx2
Set NewName=operation%1%OldName:~10%
Echo Ren %2 %NewName%
Change the path in the cd statement. The first of the stacked for loops iterates the letters A to J. The second one executes dir in the folder script_(letter) listing all files starting with operation and the extension .txt. The findstr then removes all correct names for this folder. The remaining are then arguments to the called subroutine
The subroutine builds the new name from the prefix operation followed by the folder letter and the original name from 11th place (offset 10).
As long as the echo remaines in front of the ren it only shows what it would do.
Upvotes: 0