shift file names with a batch file - batch-file

Here is the file name format. The leading number is the layer and the second number is the material (3D printer).
01118_7.tif,
01118_6.tif,
01118_5.tif,
01118_4.tif,
01118_3.tif,
01118_2.tif,
01118_1.tif,
01118_0.tif
What I need to do is shift the files ending in _1, _4, _6 six places higher. So, 01124_1, 01124_4, 01124_6 while the rest of the files stay the same. I need to do it all the way down to layer 00112_*.
I'd like to do this via a batch file if I can. Was trying to follow a guide but the name format is tripping me up.
Basic excel format

I can't tell if you need to modify file names that appear within a text file, or if you need to rename files. Either way, I have a simple solution using one of two hybrid JScript/batch regex utilities:
Modify filenames within a text file using JREPL.BAT:
jrepl "^\d{5}(?=_[146]\.tif)" "lpad(Number($0)+6,'00000')" /i /j /f test.txt /O -
Rename files within the current directory using JREN.BAT:
jren "^\d{5}(?=_[146]\.tif$)" "lpad(Number($0)+6,'00000')" /i /j
Use call jrepl or call jren if you put the command within a batch script.

It took me awhile to understand that "shift file names six places higher" really means "add six to file name".
#echo off
setlocal EnableDelayedExpansion
set "numbers=/1/4/6/"
for /F "tokens=1,2 delims=_." %%a in ('dir /B /A-D *.tif') do (
if "!numbers:/%%b/=!" neq "%numbers%" (
set "newNum=1%%a+6"
ECHO ren "%%a_%%b.tif" "!newNum:~1!_%%b.tif"
)
)
If the names are not in files, but are lines of text placed in a text file, change the 'dir /B /A-D *.tif' command by the name of the text file.

Related

for /f Not Reading Text File List

I can't seem to get my batch file to copy only the files listed in a .txt file.
Inside the source folder are three PDF files.
They are apple.pdf, bat.pdf, and cow.pdf.
The text file is two lines containing bat.pdf on the first, and cow.pdf on the second. There is no punctuation, spaces, or anything.
The batch, the list, and the two folders are on my desktop.
#echo off
set src=C:\Users\weasel\Desktop\source
set dest=C:\Users\weasel\Desktop\dest
set list=C:\Users\weasel\Desktop\animallist.txt
for /f %%F in (%list%) do robocopy "%src%" "%dest%" *.pdf /R:1
I want only the animals in the list to be copied, but the code is just copying all .pdf files it can find as if only the robocopy was there alone. I end up with apple, bat, and cow in my destination folder.
I seem to be messing up the "for /f %%F in (%list%)" part, but I don't really know this coding and I can't find anything online that dumbs it down enough for me.
Any help would be greatly appreciated. Thank you.
You haven't included the metavariable in the list. *.pdf means "any file with a .pdf extension."
Try %%F*.pdf instead - or perhaps %%F.pdf if you file is jaguar.pdf and you want only that file.
Note also
The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned.
Use for /f "usebackq" %%F in ("%list%") to allow a filename containing spaces to be processed
Use for /f "delims=" %%F in (%list%) to allow a data lines containing spaces to be assigned to %%F
Use for /f "usebackqdelims=" %%F in ("%list%") to combine the two

Delete semi duplicate files

I'm wondering if there is a way to remove semi-duplicate files (name based) using a batch file or any other means (freeware utility) in Windows?
To give an example I have following files in a directory:
fileNameXXX(aaa).ext
fileNameXXX(bbb).ext
In this case, I only want to keep the fileNameXXX(bbb).ext
it's a single line in batch:
for /f "delims=" %%f in ('dir /b "*(*).ext" ^| find /v "(ddd)"') do ECHO del "%%f"
For every file matching the filemask excluding files with (ddd) do: delete it.
Remove the ECHO if the output fits your needs.
Note: if you want to use it directly on command line (instead in a batch file), replace every %%f with %f.
Tip: think about using some more code to check, if there is a Dutch version, and if not, keep the English one (or whatever you prefer).

How to selectively delete header lines from a .csv file using Windows batching (cmd)?

I am new to CMD batching and I am writing a small script that should take a file containing a list of .csv files and remove the header of every file except for two lines. One of the lines to keep contains the word "Timestamp", it looks like this:
"Timestamp: 2013-10-31T16:26:48.0110000".
The other line to keep signals the end of the header and it always starts with "Time(ms)" as the first token.
Initially I assumed the header to be of constant size (lines 1-20) and just kept line 2 and 20. Now I need my script to actually detect the end of the header as there are files with a 24-lines header.
Here is the script as it is right now
#if (#X)==(#Y) #end /* Harmless hybrid line that begins a JScript comment
setlocal
#echo off
for /f "tokens=*" %%a in (list.txt) do (
findstr /n "^" "%%a"|repl "^(1|10|11|12|1?[3456789]):" ":"|repl "^\d+:" "" A >"%%a.new"
move /y "%%a.new" "%%a" >nul
)
Credits to #dbenham who helped a lot to wrote the first script in an answer to a previous question and publish an utility named repl.bat.
Thanks in advance
David RODRIGUEZ
Here is a solution that should be robust. It uses a tool called findrepl.bat which is a grep-like tool written in a manner similar to repl.bat.
This uses a helper batch file called findrepl.bat - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
Place findrepl.bat in the same folder as the batch file or on the path.
#echo off
for /f "delims=" %%a in (list.txt) do (
type "%%a"|findrepl "^" /e:"^Time\(ms\)" /b:"Timestamp" >"%%a.new"
type "%%a"|findrepl "^Time\(ms\)" /o:0:999999 >>"%%a.new"
move /y "%%a.new" "%%a" >nul
)
pause
This assumes that Time(ms) is in column 1 and in theory it will handle files with up to 1 million lines.

Batch read and rename from text file issue?

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

How to call a batch file from batch file

I need the syntax to call a batch file from the first batch file. The second batch files name changes with the revision. so i have only half of my second batch file name.
How do i search in a particular folder and call the second batch file..`?
Don't know the revision prefix or suffix, but you could try something like this:
for /f "tokens=1" %%n in ('dir /on /l /b /a-d "bat_file_*.bat"') do set latest_bat_file=%%n
It's relying on dir /on to sort by name, so it puts the last entry alphabetically in the variable %latest_bat_file%. You can then call it with:
call "%latest_bat_file%"
This assumes there are no spaces in your bat file names and that the revision is a numeric or alphabetical suffix. If you're using numbers, to avoid sorting problems, prefix your revision names with zero's (e.g. bat_file_001, bat_file_002).
FOR /R <path> will walk the directory tree for you.
FOR /R "%DIR_TO_SEARCH%" %%b IN (matching_*.bat) DO cmd /c "%%~b"
cmd /c will create a new shell instance, which means that if the invoked .BAT file sets environment variables, they won't be changed in calling script. This is usually what people want. If you actually wanted those side effects preserved, you could use call "%%~b" instead.

Resources