Batch rename file with exclamation mark based on folder's name - batch-file

I have a folder with photos that has more sub-folders which have dates as name.
For instance it is something like C:\Users\Lorem\Desktop\Photos\22-Dec-98\.
And inside each date folder I have photos that have short descriptions, e.g Third morning on site.png, but some may contain exclamation marks (!).
I managed, by looking at other questions, to come up with a program that adds the date in front of each short description, e.g. 18-Jun-98-Weekend campfire.png, which is what I wanted, but it fails to do so when the short description has an exclamation mark.
#echo OFF
SET /P folderPath="Enter full path of the folder which has the files you want to rename: "
PAUSE
SETLOCAL EnableDelayedExpansion
PAUSE
FOR /F "tokens=*" %%G IN ('dir /b %folderPath%') DO (call :subroutine "%%G")
PAUSE
:subroutine
set date=%1
set date=%date:~1,-1%
set newPath=%folderPath%\%date%\
FOR /F "tokens=*" %%F IN ('dir /b %folderPath%\%1\') DO (call :rename "%%F")
:rename
set fileName=%1
set fileName=%fileName:~1,-1%
set name=%date%-%fileName%
set noname=%newPath%%date%
REN "%noname%" "%name%"
I know the approach that requires input of the folder location is probably not the best option but it was the best workaround I could come up with. All in all, what should I change in/add to my code for it to rename files that contain exclamation marks as well? I would highly appreciate explanations to any changes, if possible, since I am not all that knowledgeable about batch scripting; I only adapted code from different questions/answers. Also I really fail to completely understand what %1 and %%F mean or hold as values so any link to tutorials/short explanation are welcome.
Thanks in advance!

Related

batch variable exclamation points used in dir | findstr

I'm trying to find files that do not match (at the beginning of the filename) predefined formats contained in a .txt file.
I have the following:
#Echo off
chcp 1254>nul
setlocal DisableDelayedExpansion
for /f "usebackq tokens=1,2,3* delims=~" %%f in ("%USERPROFILE%\Desktop\xref.txt") do (
set "DIRNAME=%%f"
set "DIRNAM2=^%%f"
set "PATHNAM=%%h"
set "ALBUMNM=%%g"
SETLOCAL EnableDelayedExpansion
IF EXIST !PATHNAM!!DIRNAME! (
PushD !PATHNAM!!DIRNAME!
dir /b /a-d "*" | findstr /v /r /c:"!DIRNAM2! -*"
)
ENDLOCAL
)
pause
EXIT /b
This works great except with filenames containing bangs (exclamation points).
Here's a sampling of my .txt file (subdirectory~album name~path) which gets generated by a script:
12 Byzantine Rulers. The History of The Byzantine Empire~12 Byzantine Rulers. The History of The Byzantine Empire~g:\test\
17th Century Poetry~17th Century Poetry~g:\test\
1984 (George Orwell)~1984 (George Orwell)~g:\test\
1_2_1~1_2_1~g:\test\
21st Century American Foreign Policy~21st Century American Foreign Policy~g:\test\
99% Invisible~99% Invisible~g:\test\
Communication Matters. That’s Not What I Meant!~Communication Matters. That’s Not What I Meant!~g:\test\
There are hundreds of directories containing hundreds of files (podcasts). I'd like to fix this batch so it can also handle bangs (!).
Thx in advance.
Edit. My test data wasn't robust enough. The findstr command also doesn't work with (at least) the following characters: é’»¿ ... that is to say PushD gets me to the right directory, but FindStr doesn't do it's culling as expected.
I don't think that the issue is necessarily code page or encoding related, and less so exclamation marks, (bangs). The major issue I see is that your text file content uses smart quotes, (curly), instead of dumb quotes, (straight). Additionally you have % characters which in batch files usually require doubling. For those reasons I would first suggest that you try to replace those characters.
For example:
#Echo Off
SetLocal DisableDelayedExpansion
For /F "UseBackQ Tokens=1-3 Delims=~" %%G In ("%USERPROFILE%\Desktop\xref.txt")Do (
Set "SUBDIRN=%%G"
Set "ALBUMNM=%%H"
Set "PATHNAM=%%I"
SetLocal EnableDelayedExpansion
Set SUBDIRN=!SUBDIRN:%%=%%%%!
Set ALBUMNM=!ALBUMNM:%%=%%%%!
Set PATHNAM=!PATHNAM:%%=%%%%!
Set SUBDIRN=!SUBDIRN:’='!
Set ALBUMNM=!ALBUMNM:’='!
Set PATHNAM=!PATHNAM:’='!
Set SUBDIRN=!SUBDIRN:“="!
Set ALBUMNM=!ALBUMNM:“="!
Set PATHNAM=!PATHNAM:“="!
Set SUBDIRN=!SUBDIRN:”="!
Set ALBUMNM=!ALBUMNM:”="!
Set PATHNAM=!PATHNAM:”="!
If Exist "!PATHNAM!!SUBDIRN!\" (
PushD "!PATHNAM!!SUBDIRN!"
Dir /B/A-D|FindStr /IVRC:"^!SUBDIRN! -"
)
EndLocal
)
Pause
Exit /B
I'm not sure how a copy of this code, within the code box will handle the smart quotes, but I'm sure you'll get the idea.

Rename Multiple Files in a folder using batch script

I have a folder called TEST. Inside there are 30 files.
Example:
DIM1_UPI_20170102.TXT
DIM2_UPI_20170908.TXT
DIM3_UPI_20180101.TXT
...
I have to rename them by removing the date tag
Exapmple:
DIM1_UPI.TXT
DIM2_UPI.TXT
DIM3_UPI.TXT
Can you please help me writing this in batch file?
Assuming your files are all starting with DIM
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "*.TXT" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-9!%%~xi
)
Once you can confirm that it does what you want, and ONLY then, remove the echofrom the last line to actually rename the files.
Important Note. If you have files with similar names, but different date entries, this will not work as you think. as Example:
DIM2_UPI_20170910.TXT
DIM2_UPI_20170908.TXT
The names are the same, but dates differ, making each filename Unique. If you rename them, there can be only 1 DIM2_UPI.TXT So as long as you understand this, you will be fine.
Edit: based on Amazon drive question. Note you need to change the directory portion to how you access amazon drive.
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "DIM*" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-16!%%~xi
)

Pull string from text file and rename the file using that text. For /F? Delims?

Hello I am a little out of my depth here so hence the question. I have tried reading past post but due to my unfamiliarity I am unable to adapt other solutions into mine. I also don't really have proper code in place but I'm hoping that because my question seems simple maybe someone can help me or direct me. Thank you.
I have a program that exports EDI data into a file called FOUT_R.edi and I need a batch script that will look into the file, pull the string from line 4 value 3 and line 7 value 3 and then rename the file using the combined values. The values are asterisks delimited. Below is a sample...
ISA*00* *00* *ZZ*JK60 *ZZ* MFG *:
GS*IN*JK60* MFG*20170223*1531*1*X*004010
ST*810*0001
BIG*20170223*237454
CUR*SE*CAD
REF*SI*238972
N1*ST*VEHICULOS COMERCIALES MEXICO S.A. DE C.V*92*065
N1*SU*METAL*92*JK60
DTM*011*20170223
So in the end I want the file name to read
"237454 VEHICULOS COMERCIALES MEXICO S.A. DE C.V.edi"
(.edi being the file extension). Again not sure what I'm doing, my starter scripts look like this
for /f "delims=*" %%fname1 IN (FOUT_R.edi) DO ren FOUT_R.edi %%fname1.edi
but it's obviously wrong it was just a starting spot for me but I realize now that this is out of my league.
Can someone please help or direct me to a site where the most inexperienced scripters can be explained in baby steps?
thank you.
Provided the name in line 7 doesn't contain colons, this should do:
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
set "FileName=FOUT_R.edi"
set "NewName="
for /f "tokens=4 delims=:*" %%A in (
'findstr /N "^" "%FileName%" ^|findstr "^4: ^7:"'
) Do Set "NewName=!NewName! %%A"
Echo Ren "%FileName%" "%NewName:~1%.edi"
timeout -1
If the output looks ok, remove the Echo
#echo off
setlocal
set "filename=FOUT_R.edi"
for /F "skip=3 tokens=3 delims=*" %%a in (%filename%) do set "Line4value3=%%a" & goto break1
:break1
for /F "skip=6 tokens=3 delims=*" %%a in (%filename%) do set "Line7value3=%%a" & goto break2
:break2
ren %filename% %Line4value3%%Line7value3%.edi
Sorry, no explanations; look for they yourself: open a command-prompt session and type the command followed by /?.
Note that if the file name may contain spaces, it must be enclosed in quotes.

finding textfile name by pattern and moving to newly created folder

I need some help with batch files as I have not done it before. I need to find textfiles with a matching pattern and move them to a class folder. I have a couple of folders that I am browsing through. Can anyone help me to modify the code to move the textfiles together?
This is what i research and piece together.
#echo off
setlocal enabledelayedexpansion
set file=name.txt
set foldername=class
set location=blockB
for /f "tokens=* delims=" %%a in (!file!) do (
set folder=%%a\public\
if not exists "%CD%\!folder!\!foldername!" (mkdir "%CD%\!folder!\!foldername!") ELSE (call)
dir /b "!folder!" | findstr /r /c"!location!"
)
my name.txt contains (adam,ben,charlie) 1 name per each line. So technically i want to move text files in (\adam\public) which contains blockB in the textfile name to a newly created folder call class (\adam\public\class). i want it to do the same for (\ben\public) and (\charlie\public). If I have any mistake in the way i code pls pardon me. Thanks.
Reason why I did not use a full path is because I am going to use it on different computers with same folder configurations.
I guess you are trying to accomplish this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=name.txt"
set "FOLDERNAME=class"
set "LOCATION=blockB"
for /F "usebackq eol=| delims=" %%F in ("!FILE!") do (
set "FOLDER=%%~fF\public"
mkdir "!FOLDER!\!FOLDERNAME!"
move "!FOLDER!\*!LOCATION!*.txt" "!FOLDER!\!FOLDERNAME!\"
)
endlocal

Batch Script assistance needed

Happy Friday Think-Tank!
I need some assistance with a Batch .BAT script. Specifically I need help with some "IF statement syntax"
I have a script that is renaming files. There are two files, one ending in four digits and the other ending in five digits. The files will be renamed with variables I have already pre-set earlier within my script.
So here is a scenario: We have two files in a directory located at
c:\Users\username\Desktop\test-dir
There are two files within test-dir:
file1.12345
file2.1234
A four digit ending is one variable type (VAR1), whereas a file ending in five digits is another variable type (VAR2).
I need an if statement to:
a) read all the files(s) with the chosen directory (without using a wildcard if possible).
b) determine based on the number of digits after the "." which variable to use.
c) once making that determination rename the file with the appropriate variables.
The final re-naming convention is as so: yyyymmddtype.1234/12345
So basically it would use the datestamp variable I already created, the type variable I already created to be injected by the if statement, and append with the original ending digits of the file.
I know this seems like a lot, but I am more so a bash script guy. I have all the elements in place, I just need the if statement and what feels like a for loop of some kind to tie it all together.
Any help would be great!
Thank you!
Sorry, not the option you where asking for. Instead of iterating over the full list checking each file for extension conformance, iterate over a list of patterns that will filter file list, renaming matching files with the asociated "type"
for %%v will iterate over variable list, for %%a will split the content of the variable in pattern and type, for %%f will generate the file list, filter with findstr using the retrieved pattern and rename matching files with the corresponding "type"
Rename command is preceded with a echo to output commands to console. If the output is correct, remove the echo to rename the files.
#echo off
rem Variables defined elsewhere
set "folder=c:\somewhere"
set "timestamp=yyyymmdd"
rem Rename pattern variables in the form pattern;type
set "var1=\.....$;type1"
set "var2=\......$;type2"
set "var1=\.[^.][^.][^.][^.]$;type1"
set "var2=\.[^.][^.][^.][^.][^.]$;type2"
setlocal enableextensions disabledelayedexpansion
for %%v in ("%var1%" "%var2%") do for /f "tokens=1,* delims=;" %%a in ("%%~v") do (
for /f "tokens=*" %%f in ('dir /a-d /b "%folder%" ^| findstr /r /c:"%%~a"') do (
echo ren "%folder%\%%~f" "%timestamp%%%~b%%~xf"
)
)
endlocal
#ECHO OFF &SETLOCAL
set "yyyymmdd=yyyymmdd"
set "VAR1=VAR1"
set "VAR2=VAR2"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\....."') do echo(ren "%%~a" "%yyyymmdd%%VAR1%%%~xa"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\......"') do echo(ren "%%~a" "%yyyymmdd%%VAR2%%%~xa"
remove echo( to get it working.
If I understand you then this will rename the two files using preset variables for each one:
for %%a in ("%userprofile%\Desktop\test-dir\*") do (
if "%%~xa"==".12345" ren "%%a" "%variableA%-%variableB%%%~xa"
) else (
ren "%%a" "%variableC%-%variableD%%%~xa"
)
)

Resources