Say I have the following files:
chromadenoisecfg5 - Copy.cpp
chromadenoisecfg5 - Copy.h
chromadenoisetest5 - Copy.cpp
chromadenoisetest5 - Copy.h
imagecfg5 - Copy.h
What I'd like to do is rename them to:
chromadenoisecfg6.cpp
chromadenoisecfg6.h
chromadenoisetest6.cpp
chromadenoisetest6.h
imagecfg6.h
What I've got so far is dir /b | findstr /i "*\b5 - Copy\b*", which lists all the files I'm interested in, but I'm not sure how to further process them in terms of renaming.
Furthermore, the solution revolves around the string 5 - Copy. How can this be achieved in a generic way, with the string passed as an argument?
Here is another batch one-liner:
for /F "delims=" %%A in ('dir /B /A:-D "*5 - Copy.*"') do #(set "NAME=%%A" & setlocal EnableDelayedExpansion & ren "!NAME!" "!NAME:5 - Copy=6!" & endlocal)
This simply replaces the partial string 5 - Copy by 6 using the sub-string replacement syntax. For this to work, delayed expansion is necessary.
If you want to enter it in command prompt (cmd) directly, use this:
for /F "delims=" %A in ('dir /B /A:-D "*5 - Copy.*"') do #(set "NAME=%A" & cmd /V /C ren "!NAME!" "!NAME:5 - Copy=6!")
Batch oneliner - UNTESTED:
for /f "tokens=*" %A in ('dir /B') do for /f "tokens=1-3 delims=-. " %a in ("%A") do ren %A %a.%c
Related
As specified in the Question title, I have the following DIR command to get most recently modified files in any directory provided the command or script with this command in running in the same directory whose files list is to be filtered.
dir /b /o:-d
But the quirk here is that I want to limit the number of files in the most-recently-modified files list such that I can run for loop on each. This list needs to be generated without using multiple FOR loops, which I can't think of any way to figure out. I can loop thro' the whole list like below:
for /F "delims=" %%a in ('dir /b /o:-d') do echo %%a
How do I reliably stop this loop strictly when most recent n files are already listed ??
I suggested you could use a prior example and modify it to accept 6 such as below where you can add a number via command line, you attempted to make a change but would be better off using this version with !count! rather than trying %%
Latest.cmd
usage: latest 6
#ECHO OFF
setlocal EnableDelayedExpansion
set "j=0"
set "count=%1"
FOR /f "delims=" %%i IN ('dir /b /a-d /o-d *.*') DO (
echo %%i
set /A j=j+1
if !j! geq !count! (
goto :end
)
)
:end
For a single line showing first 6 but continuing without break that translates to
cmd /V:ON /c "set "latest=6" & set "j=0" & echo/ & for /f "tokens=*" %F in ('dir /b /a-d /o-d *.*') do #set /A "j=!j!+1" >nul & if !j! leq !latest! echo %F"
A much simpler version for break after say 6 files would be a modification to dbenham's answer
#echo off
set n=0
set count=6
for /f "usebackq delims==" %%F in (`dir /b /a-d /o-d *.*`) do (
echo %%F
set /a "n+=1, 1/(%count%-n)" 2>nul || goto :break
)
:break
set n=
set count=
For a cmd one line command it needs to be a bit more convoluted Change the 6 towards the end, to your desired number. (I am sure others will do better! since I am nesting cmd from the cmd line)
cmd/r "#echo off&echo/&for /f "usebackq tokens=*" %F in (`dir /b /a-d /o-d *.*`)=do=(echo=%F&set /a "n+=1,1/(6-n)" 1>nul=2>nul||(exit /b))"
Another way to do it in a cmd batch-file.
SET "FILECOUNT=6"
powershell -NoLogo -NoProfile -Command ^
(Get-ChildItem -File ^| Sort-Object -Property LastWriteTime ^| Select-Object -Last %FILECOUNT%).Name
I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause
I'm using window OS, and I downloaded video as series lesson. I would like order those videos by date and adding prefix number for first video as 1 than 2, 3..... I found answer at here How to rename and add incrementing number suffix on multiple files in Batch Script? it can add number to filename, but can only order by name not date. Thank and sorry for my English.
Here is a simple implementation of what aschipfl suggested in his comment:
#echo off
setlocal enableDelayedExpansion
set /a n=0
for /f "delims=" %%F in ('dir /b /od') do (
set /a n+=1
ren "%%F" "!n!_%%F"
)
But there are two potential problems:
1) The FOR /F command has a default EOL character of ; - if a file name were to start with ;, then it would be skipped. The simple solution is to set EOL to a character that cannot appear in a file name - : is a good choice.
2) The rename will fail if a filename contains a ! character because delayed expansion is enabled, and %%F is expanded before delayed expansion. The solution is to save the file name to a variable, and then toggle delayed expansion ON and OFF within the loop
#echo off
setlocal disableDelayedExpansion
set /a n=0
for /f "eol=: delims=" %%F in ('dir /b /od') do (
set "file=%%F"
set /a n+=1
setlocal enableDelayedExpansion
ren "!file!" "!n!_!file!"
endlocal
)
There is a simpler option - use FINDSTR /N to prefix the DIR /B /OD output with the line number, and then use FOR /F to parse out the number and the file name. Now there is no need for a counter variable or delayed expansion.
#echo off
for /f "delims=: tokens=1*" %%A in ('dir /b /od ^| findstr /n "^"') do ren "%%B" "%%A_%%B"
You don't even need a batch file. You could use the following directly from the command line:
for /f "delims=: tokens=1*" %A in ('dir /b /od ^| findstr /n "^"') do ren "%B" "%A_%B"
I'm trying to run a simple batch file.
for /f %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
this only works if there is no space in the path of the batch file.
the batch is in:
C:\Users\Rafael\Documents\Visual Studio 2012\Projects\vorace\Release\vorace.exe
the files I want to loop through are in:
C:\Users\Rafael\Documents\Visual Studio 2012\Projects\vorace\Release\EnsembleIndependant
can anyone help me.
thanks.
thank you guys for the suggestions.
for /f "delims=" solved my problem.
for /f "delims=" %%a IN ('dir /b /s "%~dp0EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
%~dp0 has already a backspace at the end.
for /f "delims=" %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
space is a standard delimiter in batch for loops as well as <tab> and with "delims=" you deactivate it.
FOR /F uses as standard delimiters space and tab, to avoid this you need to add the options.
for /f "delims=" %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
i'm a newbie in batch scripting,started learning this from last week only and this is the first question that i'm asking here.here is my situation,
Consider this example,this lists all directories under D:/Jose/test1 and append this to a text file.
Code:
#echo off
SETLOCAL EnableDelayedExpansion
cd /d D:\Jose\test1
FOR /F "delims=" %%G in ('dir /ad /on /s /b') DO (
ECHO %%~pG%%~nG>>D:\test2\list.txt
)
ENDLOCAL
pause
Text file output :
\Jose\test1\1
\Jose\test1\2
\Jose\test1\1\12
\Jose\test1\1\13
\Jose\test1\1\12\131
\Jose\test1\1\12\Copy of 131
\Jose\test1\1\12\131\1311
\Jose\test1\1\12\131\1311\13111
\Jose\test1\1\12\131\1311\13112
\Jose\test1\1\12\Copy of 131\1311
\Jose\test1\1\12\Copy of 131\1311\13111
\Jose\test1\1\12\Copy of 131\1311\13112
\Jose\test1\1\13\132
\Jose\test1\1\13\132\1321
\Jose\test1\1\13\132\1321\13211
I want to remove '\jose' from all line ie, i want to set '\test1' as the starting path. Need help guys..Thanks in advance...
try it with sed for Windows:
for /d /r %%G in (*) do sed -r "s/^\\[^\]+(\\.*)/\1/" "%%~pnxG">>D:\test2\list.txt
..solution with pure batch:
#echo off &setlocal
(FOR /f "tokens=2* delims=\" %%a IN ('dir /ad /on /s /b') DO ECHO(\%%~b)>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE
..and more batch:
#echo off &setlocal
(FOR /d /r %%G in (*) DO (
SET "fname=%%~G"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fname=!fname:*\Jose=!"
ECHO(!fname!
ENDLOCAL
))>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE