Rename only a specific part of every filename in a folder - command-line

I want to, for example, replace all -v1 parts in every filename for -v2. It's almost working, but the access is denied because the files are being used by the cmd process itself during execution. My current code is:
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do ren "%%a" "%%a:-v1=-v2"
Any suggestions? :)
EDIT:
I've also tried for /r %%i in ("*-v1*") do ren "%%~nxi" "%%~nxi:-v1=-v2" and it finds the files and uses the correct rename value, but still outputs that it can't access the file because it is in use by another process. I'm sure that process is the cmd.exe itself, because after I close the command prompt, I can change the filenames manually without any problems.
I also tried by writing the current filenames to a temporary .txt file with the idea that the files I want to rename aren't used by any command. Then read the file with the type command within a for loop to rename each file, but same thing.
It's quite frustrating, any help is appreciated :)

substring substituion doesn't work with for variables (%%a). Use a "normal" variable and delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do (
set filename=%%a
ren "%%a" "!filename:-v1=-v2!"
)

Related

Using a Batch-Script to add a date between the filename and the extension of it

I have a Script that is searching for the newest file in the directory "test" and gives it the variable "Newest".
pushd c:\Test
for /f %%i in ('dir /b/a-d/od/t:c') do set Newest=%%I
Now let's assume that the name of the file is ThisFile.txt.
I now want to rename it to ThisFile_yyyymmdd.txt by using my variable %Newest%.
What I have so far is this:
Set TDate=%date:~6,6%%date:~3,2%%date:~0,2%
ren %Newest% "%Newest%%TDATE%"
This however renames my file to ThisFile.txt_yyyymmdd which obviously removes the extension and ruins the file.
Does anyone have a solution for this?
Keep in mind that I have to rename it by using the variable %Newest%.
Here you go . Also %%i are case sensitive.
#echo off
setlocal
pushd c:\Test
Set "TDate=%date:~6,6%%date:~3,2%%date:~0,2%"
for /f %%I in ('dir /b/a-d/od/t:c') do (
echo ren %%~fI "%%~nI_%TDATE%%%~xI"
)
exit /b 0
Must read FOR/?

rename multiple files in order with command prompt

I have some files with different names.
Leviathan.txt,Dragon.txt and so on
I wanted to turn it into a digit begins
1.txt,2.txt,3.txt,4.txt and so on
how to perform like other language by using For and function that can pass amount files in folder?
my code so far i know is dir and ren. and i stuck now.
ren *.txt 1.txt
Next code snippet could work for you (save with .bat extension); note that rename command is echoed merely for debugging purposes:
#echo off
SETLOCAL enableextensions enabledelayedexpansion
set /A "ii=0"
pushd "working_directory_here"
for /F "delims=" %%G in ('dir /B /ON "*.txt" 2^>NUL') do (
set /A "ii+=1"
echo ren "%%~G" "!ii!%%~nxG"
)
popd
If you insist on an one-liner (launch in proper working directory):
cmd /E:ON /V:ON /K (#echo off^&set /A "ii=0" ^>NUL^&for /F "delims=" %G in ('dir /B /ON "*.txt" 2^^^>NUL') do (set /A "ii+=1" ^>nul^&echo ren "%~G" "!ii!%~nxG"))^&exit
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G etc. special page) Command Line arguments (Parameters)
(EnableDelayedExpansion) Setlocal EnableDelayedExpansion
(^>, %% etc.) Syntax : Escape Characters, Delimiters and Quotes
Assuming none of your existing files are already named something like n.txt, where n is a number, then simply CD to your folder, and run the following command from the command line:
for "tokens=1* delims=:" %A in ('dir /b *.txt^|findstr /n "^"') do #ren "%B" "%A.txt"
Double up the percents if you use the command within a batch script.
EDIT
I forgot about my JREN.BAT utility - a regular expression renaming utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
JREN has a built in ability to incorporate a number into each new file name, and as an added bonus, it can left pad the number with zeros so that a DIR command lists the files in numerical order. The default numeric width is 3 digits, so files would be like "001.txt", "002.txt', ... "010.txt", ... "100.txt", etc.
jren "^.*" "$n+'.txt'" /j /fm *.txt
The /NPAD option specifies the minimum numeric width, so NTAB 1 produces no padding, which is what the original question asked for.
jren "^.*" "$n+'.txt'" /j /fm *.txt /npad 1
Since JREN is a batch script itself, you must use CALL JREN if you put the command within another batch script.
Full documentation is available from the command prompt via jren /? | more. My console window is configured with a large buffer, so I can scroll back to see prior output, and I don't bother with piping the help to MORE.

Batch file to find and run latest file in a dir with only partially known name?

How can I write in a simple Windows 7 compatible Batch file:
Where filename begins with "c:\my folder\myfile*.exe", run only the most recent one created.
For example, if I have 10 files in "c:\my folder\" and they are all similarly named myfile*.exe, and myfileBOB.exe was the last of this named file to be created - how to fish this out (the folder also contains other general files of different types) automatically by filename myfile* AND created date to execute?
Many thanks!
Sort the files by date ascending, and keep the last (most recent) one.
#echo off
setlocal
pushd "c:\my folder"
set "file="
for /f "eol=: delims=" %%F in ('dir /b /a-d /od myfile*.exe') do set "file=%%F"
if defined file "%file%"
popd
Or sort files by date descending, and break out of loop after first iteration.
#echo off
pushd "c:\my folder"
for /f "eol=: delims=" %%F in ('dir /b /a-d /o-d myfile*.exe') do "%%F"&goto :break
:break
popd

Batch file : copy all file except those its name contain some substring

first of all im beginner. i want to create batch file to search through specific folder (including all it subfolder) and copy all file inside it except those which filename contain some specific string,this is what i have so far
set now=fish
set logDirectory="C:\Users\paiseha\Desktop\bb\"
for /r %logDirectory% %%i IN (*%now%*.*) do (
rem copy process goes here
)
let say i have 3 file in it
C:\Users\fareast\Desktop\bb\one.txt
C:\Users\fareast\Desktop\bb\twofishtwo.txt
C:\Users\fareast\Desktop\bb\three.txt
so i want to copy file one.txt and three.txt only, but instead it copy only the second one,i know its because of *%now%*.* so how can i invert it so that it does the other way around, help me pls, thanks in advance
try:
#ECHO OFF &setlocal
set "now=fish"
set "logDirectory=C:\Users\paiseha\Desktop\bb"
for /f "delims=" %%a in ('dir /a-d/b/s "%logDirectory%"^|findstr /riv "^.*[\\][^\\]*%now%[^\\]*$"') do (
rem copy process goes here
)
EDIT: The \ character is represented as [\\] instead of \\ because of a quirk on how Vista FINDSTR regex escapes \. Vista requires \\\\, but XP and Win 7 use \\. The only representation that works on all platforms is [\\]. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.
for /f "delims=" %%a in ('dir /a-d/s/b "%logDirectory%" ') do echo %%~nxa|findstr /i /L "%now%" >nul&if errorlevel 1 ECHO COPY "%%a"
should work for you.

Moving all files from MANY folders up one level - command line

I have tens of thousands of *.wav files spread out across hundreds of folders. I need help to get command line to move all files up one level. The directory structure for all these files are identical, but the names of the folders vary slightly:
Z:\Audio\Level*\story*\VOCAB\*.wav
All files are located in the VOCAB folders, and I need to move them to the story* folders:
Z:\Audio\Level*\story*\*.wav
I can do this from command line by running a move command on each individual folder, but is there a way to run it recursively on all files within the entire directory? Can I use a wildcard in the location path?
Notes:
The * in Level* and story* are numbers 01-24.
I'm on Windows XP Professional.
Thanks for any help you can provide!
try something like:
for /r %F in (*.wav) do move %F %~pF\..
refer to for /? from command prompt as reference (particularly in case I didn't 'code' that quite right...)
I suggest starting it from within \Audio directory.
Found a similar question in another forum (http://www.computerhope.com/forum/index.php/topic,98046.0/all.html).
Modifying some of their code, here's a batch file script that might do the trick (please try on a small subset before unleashing it on everything):
#echo off
set thisdir=%cd%
for /f "delims=" %%A in ('dir /b /ad') do (
cd /d "%%~dpnA"
for /f "delims=" %%B in ('dir /b /ad') do (
echo Level 2 Directory: %%~dpnB
cd /d "%%~dpnB"
for /f "delims=" %%C in ('dir /b /ad') do (
echo Level 3 Directory: %%~dpnC
cd /d "%%~dpnC"
move *.* ..\
cd ..
rd "%%~dpnC"
)
cd ..
)
cd..
)
cd /d "%thisdir%