Using windows REN command to remove a prefix from filenames - windows

I'm trying to come up with a solution for this issue using only windows cmd line if it's possible.
I have a series of files that look like the following,
[sometexthere233] Tv episode 1
[sometexthere233] Tv episode 2
[sometexthere233] Tv episode 3
I would like to detect any file names in the current directory that contain text within brackets as the prefix, and remove that portion of the file name.
Tv episode 1
Tv episode 2
Tv episode 3
I've done some research using the windows REN command, but I can seem to approach the right syntax or wild card for it to execute.
Any help on how to do this, or to create a bat file that is able to do this would be greatly appreciated.

The following script searches the current directory for files matching the mask [*] * and renames them by removing the bracketed part and the space after it:
#ECHO OFF
FOR %%F IN ("[*] *") DO CALL :process "%%F"
GOTO :EOF
:process
SET oldname=%1
SET "newname=%~nx1"
SET "newname=%newname:*] =%"
RENAME %oldname% "%newname%"

Related

How to rename files with inserting a hyphen after second space separated file name part?

I'm not great at coding in any way. I figured out that I would need to start using some code to make some tasks a lot less tiresome.
I'm currently using Plex TV which is a media server that allows you to upload TV series and movies to the online server and you can then beam it directly to a TV from a phone app. (How awesome right!)
I'm trying to upload the files, but it needs to match a certain path for it to work properly.
E.g.: TV Shows\Greys Anatomy\Season 1\Greys Anatomy - S01E01\
I've got 9 seasons worth of Greys Anatomy, but the file names are missing the hyphen: Grey's Anatomy S01E0
Is there a way to rename MULTIPLE files and insert - between Anatomy and S01E01.
I have 9 seasons without the hyphen.
I've looked at a command such as ren "Greys Anatomy "* "Greys Anatomy - "*, but it isn't working. It replaces the following S and then it screws it all up. I'm sure there is an easy solution, but I'm not good at coding and have been trying for an hour or two now.
The command syntax is: Command name ren, SPACE, argument string for file(s)/folder(s) to rename optionally with path enclosed in double quotes, SPACE, argument string for new file/folder name(s) enclosed in double quotes always without path.
So the correct command line to use would be with wildcard * inside the double quoted first and second argument strings:
ren "Grey's Anatomy *" "Greys Anatomy - *"
But this command line does not work as you want it. The number of characters in current file name up to * are 15 while the number of characters in new file name up to * are just 14. For that reason command REN takes also the next character S and renames Grey's Anatomy S01E01 with this command line to Greys Anatomy - 01E01 with S missing.
I suggest to use a batch file with following code on which the second line must be adapted by you as you have not posted full qualified path of the files to modify:
#echo off
set "SourceFolder=C:\Temp\TV Shows\Greys Anatomy"
for /F "delims=" %%I in ('dir "%SourceFolder%\Grey*Anatomy*" /A-D-H /B /S 2^>nul') do (
for /F "eol=| tokens=2*" %%A in ("%%~nxI") do ECHO ren "%%I" "Greys Anatomy - %%B"
)
set "SourceFolder="
pause
Note: There is ECHO left to command ren in fourth line to just output the rename command instead of really doing the rename. Run this batch file and verify if the output rename command lines would produce what you expect. On a positive result remove ECHO , save the batch file and run it once again to really rename the files.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
for /?
pause /?
ren /?
set /?
By the way: There are GUI applications like shareware file manager Total Commander which make it possible to do such file rename operations also for people with no experience in writing code using just the computer mouse.

Batch file if then statement from user input

Brand new here, and brand new to scripting. So, I'm struggling with this and I know I shouldn't be.
I have a batch file that currently scans and unpacks zip files. All I'm trying to do is add an input line for Business Date at the top that can then be referenced in the unpacking to make sure the dates are correct. Here is the file as it exists (I've added the user input just don't know how to reference it):
echo off
:Ask
echo Please enter Business Date (MMDDYYY)
set INPUT=
set /P INPUT=Type input: %=%
c:
cd\502
erase *.prn
7z e "\\svr-dc\ftp site\502\daily\data1.zip"
copy sz*.* sales.xls
copy sc*.* cosales.xls
copy aj*.* money.xls
copy cc*.* count.xls
pause
Instead of a pause there I would like for the file to run through all the directories, they are 502 to 607, and tell me whether the filename listed in the . is the same as the user entered business date. Then, run through the rest of the directories doing the same thing. Once it's verified that I'd like to have a .txt file open with any business dates that didn't match. Does that make any sense? Any help would be appreciated.
Thanks so much.
...
c:
FOR /L %%X in (502,1,607) DO (
cd\%%X
erase *.prn
7z e "\\svr-dc\ftp site\%%X\daily\data1.zip"
copy sz*.* sales.xls
copy sc*.* cosales.xls
copy aj*.* money.xls
copy cc*.* count.xls
)
You would use %input% to access the value that was entered as INPUT, but you give no clue to how "to make sure the dates are correct."
Hmm - given further information (perhaps...)
...
c:
FOR /L %%X in (502,1,607) DO (
cd\%%X
erase *.prn
7z e "\\svr-dc\ftp site\%%X\daily\data1.zip"
if exist sz%input%*.prn (
copy sz*.* sales.xls
copy sc*.* cosales.xls
copy aj*.* money.xls
copy cc*.* count.xls
) else (
echo Repoll %%X First>>C:\Users\Boster\Desktop\RePoll.txt
)
)
It's critical that the ( occurs on the same physical line as the if and that ) else ( all appears on the same physical line.
You asked a couple of question so instead of writing one batch script I thought I should break it down to explain how it would answer your questions:
1- Use %INPUT% to reference your variable/user input
2- If you will always have 502 to 607, then you can loop through them using a for loop
FOR /L %%G IN (502,1,607) DO echo %%G
3- To check if there exist a file/folder with the same value as the user input use an if EXIST %INPUT% statement
There are quite a few tutorials out there, here is one that walks you through batch file scripting

Bulk renaming files in relation to the folder names

I am very new to coding and bulk processes but i am looking for a command line SPECIFICALLY for windows command prompt and i am wondering if such a thing exists. So I have a folder containing 111 subfolders, with each subfolder containing between 20 and 40 png image files. Each subfolder is named 001-111 accordingly and the png files are ordered how i want them, however i am looking for a command line that would be able to quickly and efficiently name all the pngs in the folders to the name of the folder followed by the png number in brackets
e.g. for folder 037, i would want the png's to be renamed to: 037(1), 037(2), 037(3) etc...
I am hoping for the best although i am unsure such a code may not be possible or be simply done.
Also if you come up with a code that achieves this process, it would be great if you could reply with the simple command line that i could use rather than a full explanation because i am new to coding and far from fluent with the language or terms or how things work. I know this same process can be achieved by going select all>rename (ctrl a>f2) and renaming to the folder name however i need to use this process frequently and dont want to have to open each folder, i would rather have a command line for cmd that would do it swiftly
Thank you and a simple answer would be greatly appreciated
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "parentdir=u:\parent"
FOR /l %%a IN (1001,1,1111) DO (
SET dir=%%a&SET "dir=!dir:~1!"
FOR /f "delims=" %%i IN ('dir /a-d /b "%parentdir%\!dir!\*.png" 2^>nul') DO (
ECHO REN "%parentdir%\!dir!\%%~nxi" "!dir!(%%~ni)%%~xi"
)
)
GOTO :EOF
Test results:
Starting directory :
u:\parent\001\1.png
u:\parent\037\1.png
u:\parent\037\2.png
u:\parent\111\999 with spaces in name.png
Script response
REN "u:\parent\001\1.png" "001(1).png"
REN "u:\parent\037\1.png" "037(1).png"
REN "u:\parent\037\2.png" "037(2).png"
REN "u:\parent\111\999 with spaces in name.png" "111(999 with spaces in name).png"
Obviously, you'd need to replace the value assigned to parentdir with your actual target directory name.
The script will report the renames it proposes to do. To actually invoke the rename remove the ECHO keyword.
I would create a batch file like so:
renamepng.bat:
cd %%1
if ERRORLEVEL 1 goto end
for %f in *.png do mv "%f" "%%1(%f).png"
cd ..
:end
This will attempt to cd to the directory name provided on the command line, abort if that fails, then rename all the .png files and return to the previous directory
then call it like so:
for %d in ??? do call renamepng.bat %d
which will loop through all 3-character file and directory names in the current directory, can call the batch file on each one. Using call instead of just the batch file name causes execution to return to the loop when the batch finishes.

Combining two FOR commands (/f /l) in a batch file

Okay guys, I am fairly new to working with batch files and I have two files I have previously created which are both working independantly.
I am looking to combine them but I still do not fully understand the FOR command. I was hoping someone could combine these two sets of code into one and if possible explain how the came up with the code they used from my two sources.
This file copies another file (in this case test.txt) to every subdirectory in a directory
FOR /R d:\ %%A IN (test.txt) DO copy d:\%username%\Desktop\Test\Resources\test.txt %%A
FOR /R h:\ %%A IN (test.txt) DO copy d:\%username%\Desktop\Test\Resources\test.txt %%A
This file copies and renames another file X amount of times (in this case 5) renaming each succesive copy in increments of 1.
For /l %%1 in (1,1,10) do (
copy test.txt test%%1.txt > nul
)
Basically I want the selected file (test.txt) to be copied from a set location to every subdirectory within a directory and then copied in each folder X amount of times and renamed with increasing values e.g.
test1.txt
test2.txt
test3.txt
etc.
Thankyou in advance.
This is actually very straight forward.
You already have functioning code that copies from the source to each subdirectory. In pseudo code: FOR (each directory) DO COPY source to target.
You also have code that can copy the file 10 times with incrementing names. You want to do this for each directory in the 1st step. So, again in pseudo code, it will look something like this:
FOR (each directory) DO (
COPY source to target
FOR (N=1 TO 10) DO COPY source to targetN
)
None of the syntax above is real, except that the parentheses after the DO are actually how you allow a batch FOR command to execute a block of multiple commands. (Actually there are other techniques to do this, but the parens work just fine.)
The part that you are missing is how to insert the incrementing number into the %%A target name. This is done by using FOR variable modifiers, as described at the end of the FOR documentation that you can access from the command line by typing HELP FOR, or FOR /?.
The modifiers allow you to deconstruct a file specification into its component parts. Note that the file doesn't have to physically exist, the file spec can still be broken down into the constituent parts.
%%~dpnA = drive:\path\baseName (no extension)
%%~xA = .extension, including the dot.
You've already got the incrementing number - I'm going to use %%N instead of %%1. So the full target will be the concatenation of the 3 components: %%~dpnA%%N%%~xA.
Putting it all together gives the full solution:
FOR /R d:\ %%A IN (test.txt) DO (
copy d:\%username%\Desktop\Test\Resources\test.txt %%A
FOR /L %%N IN (1 1 10) DO copy d:\%username%\Desktop\Test\Resources\test.txt %%~dpnA%%N%%~xA
)

How to rename and add incrementing number suffix on multiple files in Batch Script?

I have 500 files coming in and I need to first check if any file(s) exist then rename all of them regardless of what their filename is (the files are named in a different language).
No need to process them in any order.
Rename:
1. “¦X¼d¬f-20110703-¦+¦dñHÑ-ª-¦=¬¦.xls”
2. “¦X¼d¬f-20110707-¦+¡¦-+¡8.xls”
3. “¦X¼d¬f-20110707-¦+¡¦ñj¦«.xls”
4. “¦X¼d¬f-20110708-¦+¡¦¬M¼n.xls”
5. “¦X¼d¬f-20110713-¦d¼O¼n¦hÑP.xls”
.
.
.
500
To:
“TWN_CH_INV_VISIT_FORM_01.xls”
“TWN_CH_INV_VISIT_FORM_02.xls”
“TWN_CH_INV_VISIT_FORM_03.xls”
“TWN_CH_INV_VISIT_FORM_04.xls”
“TWN_CH_INV_VISIT_FORM_05.xls”
.
.
.
“TWN_CH_INV_VISIT_FORM_500.xls”
Hope you could help me on this one. I’ve been trying to do this for weeks.
a simple FOR with a count (SET /A) should do what you need.
setlocal enabledelayedexpansion
SET /A COUNT=0
FOR %%A IN (*.xls) DO (
SET /A COUNT+=1
REN "%%A" "TWN_CH_INV_VIST_FORM_!COUNT!.xls"
)
See HELP FOR and HELP SET
This is a deceptively difficult question to solve.
The 5 year old PA answer has a few problems.
1) The FOR loop begins iterating without buffering the entire directory tree, so it has the potential to rename a file that has already been renamed. I believe that is why the 7 file is missing within r0mmel's comment.
2) Delayed expansion occurs after for variables are expanded, so the file name will be corrupted and the rename will fail if the name contains a ! character.
3) A rename can fail if there already exists a TWN_CH_INV_VIST_FORM_n.xls file with the same number.
At first I thought I could solve the problem using the following:
#echo off
for /f "delims=: tokens=1*" %%A in (
'dir /b *.xls ^| findstr /n "^"'
) do ren "%%B" "TWN_CH_INV_VIST_FORM_%%A.xls.new"
ren *.txt.new *.
I use DIR /B to list the files, and pipe that result to FINDSTR to prefix each file name with a line number, followed by a colon.
I then use FOR /F to iterate and parse the results into the number and the file name. FOR /F buffers the entire result before iterating, so I don't need to worry about renaming the same file twice.
I first give the renamed files a .xls.new "extension", just in case your directory already has files that meet the TWN_CH_INV_VIST_FORM_n.xls pattern. You don't want any name collisions. The final REN command then simply removes the .new extension to leave the desired .xls.
BUT, I just noticed that the original file names have lots of weird characters that could involve unicode that is not in the current code page. FOR /F does not play well with unicode.
There is one other minor issue in that the above does not pad the number to a fixed width. (this could have been solved easily enough)
So at this point it is time to break out my JREN.BAT regular expression renaming utility. It is pure script (hybrid batch / JScript) that runs natively on any Windows machine from XP onward. It has a built in facility to incorporate a fixed width incrementing number in the new name, and it works fine with unicode. I still temporarily give the new name the ".xls.new" extension to avoid any name collisions.
#echo off
call jren "^.*" "'TWN_CH_INV_VIST_FORM_'+$n+'.xls.new'" /j /npad 3 /fm *.xls
ren *.xls.new *.
I chose to pad the incrementing number to 3 digits instead of 2 because the OP said there could be 500 files.
Full documentation for JREN.BAT is available from the command line via jren /?, or jren /?? if you want paged output.

Resources