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

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

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!

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

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"
)

Remove section of filename

I am needing a batch file to remove a certain part from multiple filenames in the same directory.
Example:
I have over 80,000+ files with the title like so:
Test Title, The - Conspiracy.zip
I am needing ", The" removed from file names leavin the titles like so:
Test Title - Conspiracy.zip
PS, I am needing this in Batch file only!
Any help is much appreciated!
THANX!!!
I found what I needed to use and thank you all for the quick replies and help!
#echo off &setlocal
set currentDirectory="%CD%"
for /f "delims=" %%a in ('dir /b /a-d *, The*.*') do (
set "fname=%%~a"
setlocal enabledelayedexpansion
set "nname=!fname:, The=!"
ren "!fname!" "!nname!"
endlocal
)
If you can use a Unix command shell, you could use the mv command in a loop.
You could download cygwin or Git Bash, or if you have Windows 10, you could do this right in the command line (assuming you've updated):
Creating a file like this
#!/bin/bash
for file in *.zip
do
removedPart=", The"
mv "${file}" "${file/removedPart/}"
done
You might want to test the command on a single file first to be sure it does what you want. i.e.
file=Test Title, The - Conspiracy.zip
removedPart=", The"
mv "${file}" "${file/removedPart/}"
You can loop through the contents of the file directory in something like this loop. Batch script loop
Then when you're looping through you can replace the contents of the file name. Look at this: String replacement in batch file
Sorry not more specific as Batch scripting isn't my thing. But this logic should prove to at least be helpful. Someone my post something better.

Almost working PDF merge batch file, just a little help needed

I'm using this batch file to merge PDF files, and it's working, but only with files which don't have spaces in their names. For a 'test file.pdf' it gives an error ~ can't find file test and file.pdf. With what should I replace the "*.PDF" to get it to work correctly?
#echo on
setlocal enabledelayedexpansion
FOR %%A IN ("*.PDF") DO (set command=!command! %%A)
%~dp0\pdftk.exe %command% cat output "%~dp1merged_PDF.pdf"
Replace set command=!command! %%A with set "command=!command! "%%~A"".
For naming the merged file after its parent folder you can determine the parent folder name like this:
for %%a in ("%~dp1.") do set "name=%%~nxa"
and then create the output file like this:
%~dp0\pdftk.exe %command% cat output "%~dp1%name%.pdf"

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.