Batch File to rename txt file - batch-file

I need to write a batch file that renames a text file with the file content. for example flie1.txt containing the word alpha becomes alpha.txt. I just some tips of what commands to use to create this batch file.

This only uses the first line or word in the file:
set/p var=<file.txt ---Setting a variable to the content inside the file.
ren file.txt %var%.txt ---Renaming the file to eh assigned variable.

windows move. unix mv
Example
Windows:
move my_original_file_name.txt my_new_file_name.txt
Unix:
mv my_original_file_name.txt my_new_file_name.txt
Your specific task in Windows:
SetLocal EnableDelayedExpansion
set your_file_location=%userprofile%
set your_file_name=!your_file_location!\file1.txt
for "eol=; tokens=1 delims=" %%n in ('type "!your_file_name!"') do (
set new_file_name=%%n
)
move "!your_file_name!" "!your_file_location!\!new_file_name!.txt"

Related

How to rename the folders with batch?

I have a file folder.txt file with a list of folder names on the HDD that have to be renamed with the names from gbv.txt. So the first line from folder.txt should be renamed with the first line from gbv.txt and so on. The folders are in the same directory from where the script will be started.
folder.txt
F-93-B-109
F-93-B-122
F-93-B-148
F-93-B-157
gbv.txt
GBV529357402
GBV52935795X
GBV529360799
GBV529362236
I'm slightly new to batch and don't know how to use rename in this situation in the loop
Essentially to read two files at a time you need to use a FOR /F command to read one file and stream the other file into the FOR /F command block. When you do this, you then can use the SET /P command to capture the line of text from the file being streamed into the command block.
#echo off
setlocal enabledelayedexpansion
< gbv.txt (FOR /F "delims=" %%G IN (folder.txt) DO (
set /p newname=
echo rename "%%G" "!newname!"
)
)
pause
The output on the screen will look like this.
C:\BatchFiles\SO>so.bat
rename "F-93-B-109" "GBV529357402"
rename "F-93-B-122" "GBV52935795X"
rename "F-93-B-148" "GBV529360799"
rename "F-93-B-157" "GBV529362236"
Press any key to continue . . .
When the output looks good, remove the word echo before the rename in the script.

Reading input from a file doesn't work for filenames containing spaces

I have a case where I have to move over 15,000 files. There are many duplicates, so I parsed them by MD5 and now I have a list of file names in an input file (files.txt).
I want to read from it, then copy the listed files to a new directory.
I pulled an old batch that someone had written as a two-part simple script and modified it.
It works for files without spaces. How can I get this to cover all file names?
Also, can't I put all of this into one file?
Cstart.bat:
for /f %%x in (files.txt) do call copyfiles.bat
copyfiles.bat:
set filename=%1
copy "C:\temp\%filename%" "C:\temp\pruned files\"
Your current code doesn't even pass the filename to copyfiles.bat, so it's not working with or without spaces. (If you need to confirm that, add echo %1 %filename & pause before the copy line in copyfiles.bat and run cstart.bat.)
With that being said, you can do it all in one file easily:
for /f %%x "tokens=1 delims=*" in (files.txt) do copy "C:\Temp\%%x" "C:\Temp\pruned files\%%x"
To make sure it works, just replace the copy in the line above with echo and run it from a command prompt.
I tested this with a text file named test.txt that contained the following:
One.txt
Two.txt
Three.txt
And Four.txt
with a batch file named testcopy.bat containing this:
#echo off
for /f "tokens=1 delims=*" %%x in (test.txt) do echo "C:\Temp\%%x" "C:\Temp\test this\%%x"
The above test showed this output:
e:\TempFiles>testcopy
"C:\Temp\One.txt" "C:\Temp\test this\One.txt"
"C:\Temp\Two.txt" "C:\Temp\test this\Two.txt"
"C:\Temp\Three.txt" "C:\Temp\test this\Three.txt"
"C:\Temp\And Four.txt" "C:\Temp\test this\And Four.txt"
for /f "usebackqdelims=" %%x in ("my file list.txt") do copy "C:\temp\%%~x" "C:\temp\pruned files"

copy file into directory based on filename

I have thousands of files to move.
I have already used a batch file to create the directories I need.
My file names look like this:
6711_05_12.pdf
10504_06_15.pdf
559_07_11.pdf
The first characters up to the "_" are the directory the files need to go into. Started the batch file - but don't know how to identify the file name.
#echo off
setlocal EnableDelayedExpansion
for %%I in (*.pdf) do (
xcopy ???
)
Is there a manual for batch files?
You don't even need a batch file. The following one line command will do the trick.
for %I in (*.pdf) do #for /f "eol=_ delims=_" %A in ("%I") do #copy "%I" "%A"
Simply double up the percents if you want to put the command in a batch file.

Assistance with coding change in Windows batch script?

I have a batch script which unzip and renames each file.
Unfortunately I now need to keep the filename of the zip file it came from.
Example Jazz1.zip now unzips and the outcoming text file becomes 1.Jazz1.zip.txt.
So I want %%F to become %%F - 4- characters.
Unfortunately I want it to be Jazz1.txt.
::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\P\DataSource2_W
SET count=1
::Action
CD "%folder%"
FOR %%F IN ("*.zip") DO (
"C:\Program Files (x86)\WinZip\wzunzip" %%F
MOVE *.txt "C:\P\DataSource2_W\TextFiles\!count!%%F.txt"
SET /a count=!count!+1
)
ENDLOCAL
I do not understand what you are trying to do with the COUNT variable, nor do I understand how you are handling a ZIP file with multiple .TXT files.
But I do understand that you want the base name of each ZIP file, (name without the extension). That is easy - simply use the ~n modifier (type HELP FOR from the command prompt for more info).
So if %%F = Jazz1.zip, then %%~nF yields Jazz1

How to delete Linefeed using batch file

I want to delete Linefeed in text file using batch file.
Is it possible to do.
Please provide some help
If you mean removing empty lines from text files try this in a batch file:
for /f "delims= tokens=*" %%x in (inputfile.txt) do echo %%x >> outputfile.txt
you may also want to replace inputfile.txt and outputfile.txt by %1 and %2 to be used for any file given its name to the batch file

Resources