Removing a filename with two extensions - batch-rename

I have many audio files with the extensions ".lossy.cue" and ".lossy.wav" and would like to remove the ".lossy" part. It seems to me the commands would be
ren "*.lossy.cue" "*.cue"
ren "*.lossy.wav" "*.wav"
but that has no effect. I know I can do it in four steps like so:
ren *.cue *.
ren *.lossy *.cue
ren *.wav *.
ren *.lossy *.wav
but then I have to move any other files in the folder with the ".cue" or ".wav" extensions first. Is there a simple way to remove this specific string in the filename without resorting to PowerShell?

you can do this:
ren *.lossy.wav *.
ren *.lossy *.wav
ren *.lossy.cue *.
ren *.lossy
Also, more information on the ren command:
https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards

Related

Using a Batch file can you remove part of a file name from files in a folder and all sub folders?

Hi I am new at using batch files and I am struggling to find a way of removing part of a file name for multiple files in a folder and all sub-folder.
the files are all named like r1_c02_200111_145423_am.csv and I need to remove the _am from the files.
I have tried the following
FOR /R "C:\Users\bob\Documents\data\" %%G IN (*_am.csv) DO REN "%%G" *.csv
but this does not change anything.
can anybody point me in the right direction please?
With the help of such a script, you can perform the required operation.
#ECHO off
FOR %%i IN (*_am.csv) DO CALL :do_rename %%i
GOTO :eof
:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"
:eof
Now more.
The search in the question was correct. To perform a rename while passing through the FOR ... DO, you can use both the SETLOCAL EnableDelayedExpansion or a procedure call. I think it's easier to use a procedure call. you don’t have to puzzle over understanding (and misunderstanding) how the SETLOCAL EnableDelayedExpansion works.
For each iteration of the loop, the Wick procedure is called with the first argument of the file name passed to it:
CALL :do_rename %%i
In the procedure itself, the argument is converted to a variable. You can already use substring operations on the variable itself. More information about substring operations can be found here.
:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"

Remove section of filename

I am needing a batch file to remove a certain part from multiple filenames in the same directory.
Example:
I have over 80,000+ files with the title like so:
Test Title, The - Conspiracy.zip
I am needing ", The" removed from file names leavin the titles like so:
Test Title - Conspiracy.zip
PS, I am needing this in Batch file only!
Any help is much appreciated!
THANX!!!
I found what I needed to use and thank you all for the quick replies and help!
#echo off &setlocal
set currentDirectory="%CD%"
for /f "delims=" %%a in ('dir /b /a-d *, The*.*') do (
set "fname=%%~a"
setlocal enabledelayedexpansion
set "nname=!fname:, The=!"
ren "!fname!" "!nname!"
endlocal
)
If you can use a Unix command shell, you could use the mv command in a loop.
You could download cygwin or Git Bash, or if you have Windows 10, you could do this right in the command line (assuming you've updated):
Creating a file like this
#!/bin/bash
for file in *.zip
do
removedPart=", The"
mv "${file}" "${file/removedPart/}"
done
You might want to test the command on a single file first to be sure it does what you want. i.e.
file=Test Title, The - Conspiracy.zip
removedPart=", The"
mv "${file}" "${file/removedPart/}"
You can loop through the contents of the file directory in something like this loop. Batch script loop
Then when you're looping through you can replace the contents of the file name. Look at this: String replacement in batch file
Sorry not more specific as Batch scripting isn't my thing. But this logic should prove to at least be helpful. Someone my post something better.

Batch file with spaces in variables/filenames in text file

I'm not at all educated on coding so you'll have to excuse any poor terminology!
I've picked a few pieces of code for a batch file from around the web (some of it from this site) and I've attempted to rewrite some of it to suit my needs.
It creates a new directory, then looks through another directory and all its subdirectories for filenames in a .txt list, and then copies those files to the directory created at the beginning. Any files that are not found from the list are written into another text file to keep track of anything that is missing.
The code is:
md 00FoundImages
set LIST=%cd%\imagelist.txt
set FILESPATH=%cd%\testimages
set DEST=%cd%\00FoundImages
for /f "delims=" %%x in (%LIST%) do (forfiles /p %FILESPATH% /s /m %%x* /c "cmd /c move /y #path %DEST%\#file" 2>>errors.txt)
This works fine under most conditions, except for when:
A filename in the text file contains a space
The path to the directory in which the batch file resides contains a space (or other special character)
I've tried enclosing in quotes like so:
set FILELIST="%cd%\imagelist.txt"
set FILESPATH="%cd%\testimages"
set DESTPATH="%cd%\00FoundImages"
This fails at the forfilescommand as soon as a space is found in the directory name.
I've tried enclosing the variable when it is used in quotes like so:
for /f "delims=" %%x in ("%LIST%") do (forfiles /p "%FILESPATH%" /s /m %%x* /c "cmd /c move /y #path "%DEST%\#file"" 2>>errors.txt)
When I run the batch file from a directory called 'testing - Copy', I get the error written into my errors.txt file
ERROR: Invalid argument/option - '-'.
Type "FORFILES /?" for usage.
This is the print of what is occurring:
E:\ImageAutomationStuff\testing - Copy>md 00FoundImages
A subdirectory or file 00FoundImages already exists.
E:\ImageAutomationStuff\testing - Copy>set LIST=E:\ImageAutomationStuff\testing - Copy\imagelist.txt
E:\ImageAutomationStuff\testing - Copy>set FILESPATH=E:\ImageAutomationStuff\testing - Copy\testimages
E:\ImageAutomationStuff\testing - Copy>set DEST=E:\ImageAutomationStuff\testing - Copy\00FoundImages
E:\ImageAutomationStuff\testing - Copy>pause
Press any key to continue . . .
E:\ImageAutomationStuff\testing - Copy>for /F "delims=" %x in ("E:\ImageAutomationStuff\testing - Copy\imagelist.txt") do (forfiles /p "E:\ImageAutomationStuff\testing - Copy\testimages" /s /m %x* /c "cmd /c move /y #path "E:\ImageAutomationStuff\testing - Copy\00FoundImages\#file"" 2>>errors.txt )
E:\ImageAutomationStuff\testing - Copy>(forfiles /p "E:\ImageAutomationStuff\testing - Copy\testimages" /s /m E:\ImageAutomationStuff\testing - Copy\imagelist.txt* /c "cmd /c move /y #path "E:\ImageAutomationStuff\testing - Copy\00FoundImages\#file"" 2>>errors.txt )
E:\ImageAutomationStuff\testing - Copy>pause
Press any key to continue . . .
From the research I've done I can see questions of this nature get asked a lot - apologies for adding to the pile but with my limited knowledge I'm afraid I'm simply unable to fix this issue on my own!
To avoid troubles with spaces in paths and file names, use quotes. But you need to be carefull when it comes to forfiles, because all path- and file-name-related #-variables are expanded to values already enclosed within "", so it can easily happen to have values double-quoted.
I would give this a try:
md "%cd%\00FoundImages"
set "LIST=%cd%\imagelist.txt"
set "FILESPATH=%cd%\testimages"
set "DEST=%cd%\00FoundImages"
for /f "usebackq delims=" %%x in ("%LIST%") do (
forfiles /p "%FILESPATH%" /s /m "%%x*" /c "cmd /c if #isdir==FALSE cd /d 0x22%DEST%0x22 & move /y #path #file" 2>>errors.txt
)
What I did:
improved syntax of set so that the entire assignment expression is enclosed within ""; this avoids trouble with some special characters, but the quotes are not part of the variable values;
quoted %LIST%, so I had to add the usebackq option to for /f, because it would treat the value of variable %LIST% as a literal string rather than a file otherwise;
the path after /p as well as the pattern after /m of forfiles are enclosed in parentheses; here it is very important that the path after /p is not terminated by a \, because this would be interpreted as an escape character by forfiles to escape the closing quote "; if you cannot assure that %FILESPATH% fulfils that criterion, write /p "%FILESPATH%\." instead; edit: the last statement is wrong!
the #path variable expands to an already quoted string, so everything is fine with it;
the tricky part is the destination file %DEST%\#file from your original code, because #file expands to an already quoted string, so the result would be something like \...\00FoundImages\"FoundImage 1.png", for instance; so quoting the entire expression like 0x22%DEST%\#file0x221 would lead to "\...\00FoundImages\"FoundImage 1.png"", hence the file name FoundImage 1.png appears unquoted to the command interpreter; using 0x22%DEST%0x22\#file1 would lead to "\...\00FoundImages"\"FoundImage 1.png", which does not harm many commands, but could impact some, and it is simply not beautiful; to overcome this, simply change to the directory %DEST% first by cd /d 0x22%DEST%0x221, so it is stated alone, then use #file only for the move command line;
finally, forfiles returns both matching files and directories, so to ensure that there are only files processed, I implemented an if query;
1)... the 0x22 expression stands for a " character in forfiles /C command expressions and represents its character code in hexadecimal notation; I prefer this way of specifying quotes, because the other option, \", might impact the command line interpreter as it does not support the backslash-escaping and therefore it recognises the quotation mark, which might alter its behaviour unintentionally.
for /f "delims=" %%x in ("%LIST%") do (forfiles /p "%FILESPATH%" /s /m %%x* /c "cmd /c move /y #path \"%DEST%\#file\"" 2>>errors.txt)
Quotes within the quotes that is the command forfiles runs have to be escaped with a backslash. But not other quotes like starting folder.
Although I do not understand why do you nest FORFILES loop in another (FOR /F) loop, next code snippet should do the trick:
for /f "usebackq delims=" %%x in ("%LIST%") do (
forfiles /p "%FILESPATH%" /s /m "%%~nxx" /c "cmd /c ECHO move /y #path \"%DEST%\"\#file" 2>>errors.txt
)
Note:
double quotes in "%FILESPATH%" and "%%~nxx";
escaped double quotes in \"%DEST%\";
plain #path and #file as those FORFILES command variables are already double quoted;
maybe that you need to reformulate /m "%%~nxx" file mask (I use %%~nxx file name and extension because I don't know %LIST% file content and suppose a list of fully qualified file names);
important: move command is merely displayed for debugging purposes; remove preceding ECHO to make it operational, no sooner than debugged.

Script copies more then *.pdf

I have a script that copies several PDF Files, and it places it to the corresponding folder.
Script:
pushd "\\share\folder\"
for %%p in (*.pdf) do for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "\\share2\folder\%%~n\%%~nxp"
)
But it also copies files that are named like this: Test.pdf2098 or hello.pdf20j93f2
I just want it to copy *.pdf files and not PDF's that are invalid.
Add an if check to verify the extension
pushd "\\share\folder\"
for %%p in (*.pdf) do if /i ".pdf"=="%%~xp" for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "\\share2\folder\%%~n\%%~nxp"
)
David Ruhmann provided a workaround, but did not explain why your code fails.
The problem is that files on NTFS volumes that do not meet the old 8.3 short file naming standard are automatically assigned alternate short file names that do meet the standard. Files like xxx.pdf2098 would be given a short name that has a .pdf extension.
The windows commands like COPY, MOVE, FOR, etc. that search file names all attempt to match both the long and the short name, thus leading to your problem.
It is possible (and often recommended) to disable short file name generation on NTSF volumes, but any existing short names will remain and potentially still cause problems.
So David Ruhmann is correct in suggesting that you verify the file extension of each file.
Another frequently used method to verify the extension is to pipe DIR /B to FINDSTR:
for /f "eol=: delims=" %%p in (
'dir /a-d /b *.pdf^|findstr /lie ".pdf"'
) do for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "\\share2\folder\%%~n\%%~nxp"
)

How do I use a for loop to get file names and then use them?

I have a folder with the files a.txt, b.txt, and c.txt in it. and I want to use a for-loop to get the names of these files, store them in a variable, and then add them to another text file. The text files are located in the same file as my bat file. Is it possible to do this? If so please show me, these for loops confuse the crap out of me...
What i have now
#echo on
SET name=hey
echo >text.txt
for %F in (*.*) do (set name=#fname
echo name >> text.txt)
#Echo OFF
(For %%# in ("*.txt") do (
Set "FileName=%%~n#"
Call Echo %%FILENAME%%
))>"MyFilenames.txt"
Pause&Exit
NOTE 1: Files are stored in "Filename" var, but is not really necessary, you can directly write the filename to the textfile.
NOTE 2:If you want a recursive loop through files use the /R switch of For command.
NOTE 3:If you want also the file extension change this: "%%~n#" to this else: "%%~nx#"
**UPDATE:**
An alternative script:
#Echo OFF
For %%# in (*.txt) do (Echo %%~n#>> "MyFiles.txt")
Pause&Exit