I have 233 files where I need to change the file names. Just look for a hyphen (-) in the filename and replace the text from right of the hyphen until dot (.) to left and left of the hyphen right.
Here is a sample filename and the pattern/naming convention used
Name Of Movie (9999) - Name of Song.kam where 9999 is the year
All files are of the same pattern. I want to reverse the order to
Name of Song - Name Of Movie (9999).kam where 9999 is the year
Note: In the filename, every word after a is in CAPS
If it is not possible, I will have to do it manually. Can someone give me a batch script to execute this or tell me if it is possible to do?
Thank you.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=1,3delims=)-." %%a IN (
'dir /b /a-d "%sourcedir%\*) - *.kam" '
) DO FOR /f "tokens=*" %%u IN ("%%b") DO ECHO REN "%sourcedir%\%%a) -%%b.kam" "%%u - %%a).kam"
GOTO :EOF
This should solve your problem. You'd need to change yur sourcedir of course.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Related
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.
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!
I have hundreds of csv files . csv files are stored in folders and sub folders . I want to search fifty csv file whose file names have been determined , for example 1.csv , 2.csv , 3.csv , ... , 50.csv . very troublesome if I searched one by one using the Windows search tool . I would like if the files are found , save in the folder named FOUND . please help to overcome this problem by using the batch programming / bat ? thank you very much
There's a number of approaches one can take, depending on how much automation you require... To help you get started, you may want to look at this it helped me (and indeed continues to do so) when I started learning batch. Furthermore I will provide one possible template for achieving your objective, as I have interpreted it. Perhaps it is not the most elegant or efficient method, but it introduces a number of batch commands that you may or may not have encountered, which in turn may help you develop your own method.
#echo off
setlocal enabledelayedexpansion
echo Please enter a drive letter:
set /p "drive=>"
echo Please enter a search string:
set /p "searchstring=>"
echo %searchstring%>search.txt
set /p search=<search.txt
set /a suffix=0
echo.>>search.txt
:LOOP
for /f "tokens=*" %%i in ("search.txt") do (
set /a suffix=suffix+1
set seq=%search% !suffix!
echo !seq!>>search.txt
)
if !suffix! leq 49 goto LOOP
for /f "tokens=*" %%i in (search.txt) do (
for /f "tokens=*" %%j in ('dir /b /s /a-d %drive%:\"%%i.csv" 2^>nul') do (
if not exist "%~dp0\found" md "%~dp0\found"
move /y "%%j" "%~dp0\found\%%~nxj"
)
)
pause
This is not intended as a definitive solution, though you may find it answers your original query/request. All the best.
Here's another working solution for you..
#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM First Set your directories input and output
SET InputDir=C:\Directory to your CSV files\
SET OutputDir=C:\Directory to your CSV files\FOUND
REM check if the FOUND directory exist, if not, then create it.
IF NOT EXIST OutputDir (
mkdir %OutputDir%
)
REM Grab a scan of the input directory and save it to a temporary file list.
Dir /a /b %InputDir%>"%OutputDir%\Found.txt"
REM Set the files you would like to find.
SET "File1=1.csv"
SET "File2=2.csv"
SET "File3=50.csv"
REM The loop, to process the matching file(s).
FOR %%A IN (%File1%,%File2%,%File3%) DO (
FOR /F "usebackq" %%B IN ("%OutputDir%\Found.txt") DO (
IF %%A==%%B (
copy "%InputDir%\%%A" "%OutputDir%\%%A"
)
)
)
REM Clean up the temp file list.
DEL "%OutputDir%\Found.txt"
Make note, I didn't add quotes to the Input and Output variables, but instead added quotes to the copy portion of the code to compensate for white spaces in your directory path. I tried to keep it simple, so you could follow the logic of how it processed what you are looking for, you can now modify this to your liking.. Have fun. Cheers!
Quite nervous today, I am a long time StackOverflow voyeur about to ask my first ever question so I hope I dont suck at it. I can perform the following task easily using something like a console app but i was hoping that it would be possible to do it in a batch script, and I dont know batch scripting well at all.
I want to delete all files in a directory except those whose name matches a certain pattern. A typical example of the kind of files in this directory is as follows:
dep_invoice_101.pdf -- to delete
dep_invoice_102.pdf -- to delete
invoice_103.pdf -- to delete
invoice_106.pdf -- to delete
invoice_106_E56.pdf -- keep
deposit_invoice_101_068.pdf -- keep
i want to delete all files except those whose name matches
any string (or nothing) followed by "invoice_"
followed by any number
then an underscore
then another alphanumeric string
The final underscore is the key. if i was doing this using C# or something, i would extract the name into a string, remove all text before "invoice_" and then see if there is a remaining underscore - this would be a match, e.g.
"deposit_invoice_101_068.pdf"
search for "invoice_" and remove all text before and including this
left with "101_068.pdf"
if there are any underscores in this string then it is a match
Hope this makes sense. If this is not an appropriate question let me know.
Many thanks
for /f "tokens=*" %%a in (
'dir /a-d /b *.pdf ^| findstr /v /r /c:"invoice_[0-9][0-9]*_.*\.pdf$"'
) do echo del "%%a"
If output to console is correct, remove the echo command
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET "filename=%%a"
SET "filename=!filename:*invoice_=!"
IF "!filename!"=="!filename:_=!" ECHO DEL "%sourcedir%\%%a"
)
GOTO :EOF
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
You'd need to change your sourcedir to suit, naturally.
This follows your how I'd do it in C rather than your narrative spec.
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"
)
)