Batch Windows - How to create a loop to rename many files in different folders? - batch-rename

I have PDF files in one location (all in the same folder), which I need to take 3 useful information from the file name.
And I have .jpg files in another location (1 picture per folder) which I need to rename with these information taken from the PDF.
My script is able to find the information, store and rename but it only works for the first file in a directory and then stops.
I need to make it run in a loop until there is either no more PDF files to take information from OR no more .jpg files to be renamed.
Can someone help me to make this script run in a loop?
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%\
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
)
rem Rename the pictures using the values stored on the variables
xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node %Node%\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node%Node%_RL%RcvLn%_GS%GunStn%.jpg"

You set each variable inside of the loop for each file, but then you do the xcopy outside of the loop which will only do the xcopy once. So we rather do the xcopy inside the loop.
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
echo xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node !Node!\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node!Node!_RL!RcvLn!_GS!GunStn!.jpg"
)

Related

name files after template bat file - used CMD code

I have a *.bat file in various folders which renames certain files so project number, project name and initials are included in the filename. So far running the file will ask the user to enter project number, project name and initials which are saved as variables (no, project and initial). Each original filename is kept but in brackets will be the required projects specific data). So far it works but what I would like is set up a batch file where the name sets the template for the project specific data e.g.filename of bat file could be:
Rename <XY00.000> (Projectname),XYZ.bat
If in CMD code you could retrieve the filename of the executable bat file (should be only 1 in the folder) and then with delimiters you would splice the filename and store the relevant strings in different variables e.g. what is inside <> will be variable "no", whats inside () will be variable "project" and what is past the , is "initial". If I have those variables the file renaming is as in my current procedure. Please help as I'm not familiar with CMD code or Powershell (which might be alternative option) and it took me long to assemble the existing code. Basically I want to change the input part (see set /p) to something which does:
get filename of bat file
extract 3 text strings from the filename (use of delimiters like ( < ,)
store the 3 text strings as parameters (NO, PROJECT , INITIAL)
the rest of the existing code should then work to rename the files in the folder
Thanks.
Existing code as example below:
cls
echo off
pushd "%~p0" 2> nul
pushd "\\%~p0" 2> nul
echo
attrib -r *.* /s
set /p no= Enter Project Reference in style XY00.000:
set /p project= Enter Project name (without any underscores):
set /p initial= Enter your Initials:
for /f "tokens=1* delims=(" %%i in ('dir /b *.xlsm') do ren "%%i(%%j" "%%i(%no% %project%_%initial%).xlsm"
for /f "tokens=1,2* delims=()" %%i in ('dir /b *.docm') do ren "%%i(%%j)%%k" "%%i(%no% %project%_%initial%)%%k"
I replaced the user input with:
set infovariable=%~n0
which safes the filename of the bat file as a variable and uses it later to rename appropriate files with it. The bat file is named exactly what I want as a text. Job done!

Using Variables in Filepaths in Batch Files

I'm trying to create a small script that will allow me to copy folders and it's contents from a certain directory on my computer to another one. So far, here's what I have:
#echo off
SET /P %TARGET%=Enter variable name:
xcopy "C:\Folder1\%TARGET%" "C:\Folder2"
pause
It returns with INVALID PATH, 0 FILE(S) COPIED.
how would I need to alter the script to fix this?
dont use % when you declare the variable :
#echo off
SET /P TARGET=Enter variable name:
xcopy "C:\Folder1\%TARGET%\*.*" "C:\Folder2"
pause

Set file name with * as variable batch script

I have a batch file that i'm having issues with. I need to find the name of a file, then set it to a variable. Then I will use this to pass it onto a vbs script to further look into the file. The name of the file is logfile_date_time.log but the time varies depending on what time is starts. The point of the batch file is to find out the last modified date of this file.
set fordate=%date:~4,2%%date:~7,2%%date:~10,4%
set filename=c:\logfile_%fordate%_*.log
if exist %filename% (goto exist) else (goto noexist)
:exist
vbsscript.vbs /file:%filename%
goto end
:noexist
file doesn't exist code blah blah
:end
pause
I had to modify the names of folders and remove some code for security purposes since this is for work.
Any help appreciated. Thanks!
not tested:
set "last_modified="
for /f "delims=" %%f in ('dir /a-d /tw /o-d /b^| findstr /r /i /c:"logfile_[0-9][0-9]*_.log"') do (
do set "last_modified=%%~dpfnxf"
goto :break_loop
)
:break_loop
if defined last_modified echo file %last_modified% exist ...
The problem with your code is that it doesn't expand the wildcard character (*), and your VBScript probably doesn't handle wildcards in filenames by itself (the FileSystemObject methods for instance don't). If the file you want to process is the only one matching your pattern, you could do something like this:
#echo off
setlocal
set "fordate=%date:~4,2%%date:~7,2%%date:~10,4%"
pushd C:\
for %%f in (logfile_%fordate%_*.log) do vbsscript.vbs /file:"%%~ff"
popd

How do I use a for loop to get file names and then use them?

I have a folder with the files a.txt, b.txt, and c.txt in it. and I want to use a for-loop to get the names of these files, store them in a variable, and then add them to another text file. The text files are located in the same file as my bat file. Is it possible to do this? If so please show me, these for loops confuse the crap out of me...
What i have now
#echo on
SET name=hey
echo >text.txt
for %F in (*.*) do (set name=#fname
echo name >> text.txt)
#Echo OFF
(For %%# in ("*.txt") do (
Set "FileName=%%~n#"
Call Echo %%FILENAME%%
))>"MyFilenames.txt"
Pause&Exit
NOTE 1: Files are stored in "Filename" var, but is not really necessary, you can directly write the filename to the textfile.
NOTE 2:If you want a recursive loop through files use the /R switch of For command.
NOTE 3:If you want also the file extension change this: "%%~n#" to this else: "%%~nx#"
**UPDATE:**
An alternative script:
#Echo OFF
For %%# in (*.txt) do (Echo %%~n#>> "MyFiles.txt")
Pause&Exit

XP Batch scripting - zipping with rinrar looping through a directory *.csv

I have read numerous articles now and it's not clear and there's lots of versions and this that and the other and I have been piecing things together and have got so far, my problem is the 'rar' command doesn't seem to accept my substition variable and instead reads it as a string.
But this is what I have
#echo off
SETLOCAL
set path=%path%;"C:\TEMP\Output"
set _sourcedir=C:\TEMP\Output
set _logfile=c:\temp\Output\zip_log.txt
set _rarpath=C:\Program Files (x86)\WinRAR
echo Starting rar batch > %_logfile%
:: Set default directory
pushd %_sourcedir%
echo Scan Directory is %_sourcedir%
FOR %%f IN (*.txt) DO (
echo %%f
%_rarpath\rar.exe a test
)
popd
ENDLOCAL
#echo on
I have cut some out and chopped it so you only get the essence, I haven't omitted any commands though.
I am trying to loop through the directory and locate all .txt files and zip them into a .rar file.
The echo writes out the correct filenames.
Any ideas?
I think this is your problem:
set _rarpath=C:\Program Files (x86)\WinRAR
In batch files, the environment variable delimiter is a space, so it thinks _rarpath is C:\Program
Enclose the path in double quotes and see if that helps:
set _rarpath="C:\Program Files (x86)\WinRAR"
Also, in your FOR loop change
%_rarpath\rar.exe a test
to
%_rarpath%\rar.exe a test
(or,perhaps this was a typo?)
I don't see where you're asking winrar to do anything with your files? %%f needs to be on the winrar command line somewhere.
Also, you shouldn't need a loop at all for this: rar.exe a test.rar %yourpath%*.csv or similar.