Batch read and rename from text file issue? - batch-file

I'm a beginner with .bat files, and I'm attempting to rename multiple drawing (.dwg) files using a list generated in notepad. The notepad list contains a list of drawing numbers that look like this:
01013-13000p001
06301-12550p001
etc..
There is hundreds of them, and I want to take those numbers from the text and put it into a blank series of dwg files that are generic named for now (drawing.dwg, drawing(2).dwg, drawing(3).dwg etc..) I've only come up with a way to read a text file, but cant figure out how to take from the text file and rename multiple drawing files with it. Below is as far as I have gotten, after failed attempts of trying to take whats read from a text file and put it into the .dwg files. I plan on working this out in all the same directory, and any suggestions will be greatly appreciated. Thanks.
#echo off
for /f "tokens=* delims=" %%x in (dwgNumbers.txt.txt) do echo %%x
pause

would
for /f "delims=" %%x in (dwgNumbers.txt.txt) do echo copy /b "blank generic drawing.dwg" "%%x.dwg"
(as a batch line) do what you want? - note that the ECHO keyword is there to show what would be done. The echo keyword needs to be removed to actually execute the copy.

This will take the numbers from .txt, renaming the existing .dwg files with the data readed. If there are more files that numbers in .txt, it will rename until number exhaustion, no more.
for loop is using a dir command to get the list of files to avoid the case of files that after being renamed falls under the filter of the for and gets reprocessed.
This code has a echo command included in rename line to prevent data loss in case of malfunction. When the output to console is what is needed, remove the echo command from the rename line.
#echo off
rem Prepare environment
setlocal enableextensions enabledelayedexpansion
rem Read input file for numbers
< dwgNumbers.txt (
rem Process dwg files in directory
for /f "tokens=*" %%f in ('dir /b *.dwg') do (
rem Get number from input file
set "number="
set /p "number="
rem If there is a number, rename the file
if defined number echo ren "%%~f" "!number!.dwg"
)
)
rem Clean
endlocal

Related

Batch file to rename files with complex names and back again

Reviving an old topic once discussed here, as I have a similar problem. The solution proposed in the old thread worked only in half in my case.
I need first to rename various media files (mp4, mp3, wav...) with irregular, sometimes complex names as 1.mp3, 2.mp4, 3.wav, etc. And some time after I need to restore the original filenames. File extensions should remain the same in both cases.
In a more specific case of renaming .raw files Helbreder proposed two .bat scripts. The first .bat changes filenames to 1.raw, 2.raw, 3.raw, etc. and creates corresponding individual .txt files each of which keeps the original filename. The second .bat takes the original filenames from individual .txt files and renames 1.raw, 2.raw, 3.raw, etc., back to the original.
For my purpose I slightly modified the first .bat proposed, and this works perfectly well:
#echo OFF
#setlocal ENABLEDELAYEDEXPANSION
set I=1
for %%G in (*.mp3 or *.3gp or *.wav or .mp4) do (
set ORIGINAL_NAME=%%~nG
set ORIGINAL_EXTENSION=%%~xG
(
REM Try to rename file
ren "%%G" "!I!"!ORIGINAL_EXTENSION!
) && (
REM Renaming was successful
> "!I!.txt" echo !ORIGINAL_NAME!!ORIGINAL_EXTENSION!
set /A I+=1
) || (
REM Renaming was a failure
echo Cannot rename [!ORIGINAL_NAME!] file.
)
)
#endlocal
Put in a destination directory, this .bat renames all media files, keeping the correct extensions, and generates a set of .txt files each of which contains the original filename with extension.
For instance, 1.txt contains a string "Play 2019-03-06 in C.mp3" which was the original filename.
Then I need to reverse the original filenames and I run the second unmodified Helbreder's .bat. For commodity purpose I paste it here:
#echo OFF
#setlocal ENABLEDELAYEDEXPANSION
for %%F in (*.txt) do (
set BASENAME=%%~nF
REM Read original name from txt file
for /F %%G in (%%F) do (
REM iterate over all corresponding files
for %%H in (!BASENAME!.*) do (
set EXTENSION=%%~xH
REM Remove dot from extension string
set EXTENSION=!EXTENSION:~1!
if not "!EXTENSION!" == "txt" (
REM Process files
(
REM try to rename corresponding file to old name
ren "!BASENAME!.!EXTENSION!" "%%G.!EXTENSION!"
) && (
REM Operation was successful - remove TXT file
del /F /Q "%%F"
) || (
REM Something went wrong
echo Cannot restore old name for file [!BASENAME!.!EXTENSION!].
)
)
)
)
)
#endlocal
As my filenames may be complex and often include blank spaces, this second .bat works in a half-successful way.
It reverted the first original filename "Play 2019-03-06 in C.mp3" written to a 1.txt with extension, as simply "Play.mp3". It also ignored a part of the second complex filename which followed blank space, keeping only "2007-03-06.mp3" instead of "2007-03-06 output.mp3". And it successfully reverted only those filenames which were composed of numbers and underscores, without blank spaces.
As far as I understand, the issue consists in the way the second .bat retrieves the filename from the text string kept in .txt file. The first blank space occurring in the text line is considered as the end of a filename.
Could you kindly suggest a solution for reverse renaming of any files from the correspondent .txt record, which may contain letters, numbers and blank spaces (and maybe special characters, like "&" and some others).
Following Compo's advise I tried another code from the old post, this time proposed by Jeb.
After a slight modification to make it match to different file types and - important! - to keep their original extensions, the code seems to work, but with issues.
When there is more than one dot in the filename the name revert code does not restore it completely, even though it is correctly saved in the .txt file by the rename batch. For example the second batch truncates "Play 2020.10.12.mp4" to "Play 2020" and does not wish to restore the extension.
Placed in directories with many files the rename batch sometimes does not rename a part of the list, sometimes does not do the job at all and sometimes renames all files flawlessly. I first thought the partial script dysfunction might be related to the presence of special signs in the filenames, like commas or parenthesis. But no, when I delete special signs, this changes nothing. Just puzzled.
So, the new version of code is only partially solving the problem. Is there something to correct in the code, to make it more universally working? Which solution could be applied in these two cases?
I divided the code into two separate .bat files to rename and to revert original filenames.
The both follow.
Rename:
#echo off
set cnt=0
del Names.txt > nul 2>&1
echo Running Batch Rename
for %%f in (*.mp3 or *.mp4 or *.3gp or *.wav) do (
set "file=%%f"
set "EXTENSION=%%~xf"
CALL :renameToNumber
)
:renameToNumber
set /A cnt+=1
set "number=00000%cnt%"
set "number=%number:~-4%"
(echo %number% %file%) >> Names.txt
ren "%file%" %number%"%EXTENSION%"
exit /b
Revert the original names:
for /F "tokens=1,*" %%A in (names.txt) DO (
set "number=%%A"
set "filename=%%~nB"
call ren %%number%%.* "%%filename%%.*"
call echo ren %%number%%.* "%%filename%%.*"
)
exit /b

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.

Batch create folders from files with similar beginning but different ending

I have a lof of files with similar beginning names, but ending different. I have to create a folder for every "series" of files, which means all files whose names begin with the same 7 characers.
See image for visualization:
The code I have right now is
for /F "Tokens=*" %%i in ('Dir /B *.jpg') do md "%%~ni"|copy "%%i" "%%~ni"
Which just creates a folder for every file and puts it inside.
Unfortunately I don't have enough knowledge of this language and just googling it only made me learn how to create a substring SET _result=%_test:~0,7% but not how to create folders with that.
I'm sure it's just a short sequence, could someone help me with that?
This may not be the most efficient way of doing this, but it should help you. The below batch script takes an input folder and output folder as inputs from user. It then iterates through all the files in the input folder and copies them to output folder under a sub-folder with first 7 characters of the filename.
#echo OFF
setlocal ENABLEDELAYEDEXPANSION
REM Get input directory from user
set /p INPUT_DIR=Please enter full path to directory with files:
REM Get ouput directory from user
set /p OUTPUT_DIR=Please enter full path to output directory:
REM Iterate through files and create necessary folders/copy files
for /f %%f in ('dir /b %INPUT_DIR%') do (
set fullname=%%f
set trimmedname=!fullname:~0,7!
set fullpath=%OUTPUT_DIR%\!trimmedname!
if not exist !fullpath! md !fullpath!
copy %INPUT_DIR%\%%f !fullpath!
)
Here's a sample output that shows the processing done by 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"

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

Resources