Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to make windows batch script to rename files that have random names. I have a folder with thousand .txt files, their names are completely random,
I want to rename first 5 files in that folder to file1.txt, file2.txt,file3.txt, file4.txt,file5.txt.
Help appreciated.
Is this okay?
#ECHO OFF
(SET f=C:\Test)
IF /I "%CD%" NEQ "%f%" PUSHD "%f%" 2>NUL||EXIT/B
SET "i=5"
FOR %%A IN (*.txt) DO CALL :SUB "%%A"
EXIT/B
:SUB
IF %i% GTR 0 REN %1 File%i%.txt
SET/A i-=1
Just change line two if your stated directory path\name has changed.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I have a file .bat that do a backup but i want to do another file .bat that remove the oldest files. Someone can help me?
set dia=%DATE:~0,2%
echo %dia%
if exist f:\exist.txt goto OK
echo KKKKKKKKKK
pause
exit
:OK
md f:\backup
md f:\backup\%dia%
xcopy d:\dat\*.* f:\backup\%dia%\*.* /s /c /h /r /e /y /j
echo TODO OK
pause
Read carefully please! The script has a safeguard echo in the rmdir line. Do not remove this until you are 100% sure the script does what you want.
#echo off
for /f "tokens=2 delims=.=" %%i in ('wmic os get localdatetime /value') do set result=%%i
set "mydate=%result:~0,8%"
robocopy "d:\dat" "f:\backup\%mydate%" /MIR /Z
for /f "skip=4 delims=" %%a in ('dir /b /ad /o-d "f:\backup\"') do echo rmdir /S/Q "%%~fa"
pause
So the script will create a folder for each date yyyymmmdd each time you run it, if the folder already exists i.e you ran the backup twice in one day, it will simply update the files and not recreate any folders if they exist.
The second for loop you have to be carefull of. it will sort the folders by decending date, i.e the latest created folders will be listed first. So you will see here I have skip=4 meaning it will skip the first 4 latest folders, and delete the rest. So if you want to keep two latest backups, then do skip=2 etc.
to amend the date to yyyymm only, change to set "mydate=%result:~0,6%". You get the idea.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I would like for someone please to help me create a script (batch/powershell maybe?) to copy multiple files from different sources and paste them in a separate private folder (MyStackedFiles for instance) from a TXT file list.
Example TXT File:
"C:\Windows\System32\File1.dll"
"C:\Windows\System32\File2.dll"
"C:\Program Files (x86)\MySoftware\Main.exe"
Example copied data output:
"C:\Users\me\Desktop\MyStackedFiles\Windows\System32\File1.dll"
"C:\Users\me\Desktop\MyStackedFiles\Windows\System32\File2.dll"
"C:\Users\me\Desktop\MyStackedFiles\Program Files (x86)\MySoftware\Main.exe"
The code that I made so far, but it doesn't make structure
#echo off
For /F "tokens=* delims=" %%a in ('type "C:\MyListwSources.txt"')
DO xcopy /hrkvy "%%a" "C:\MyFolder"
PAUSE
Thank you!
As per my comment, a single line batch file should do as you intended, (just change C:\MyListwSources.txt as necessary):
#(For /F Delims^=^ EOL^= %%I In ('Type "C:\MyListwSources.txt"')Do #"%__AppDir__%xcopy.exe" /HRKVY "%%~I" "%UserProfile%\Desktop\MyStackedFiles%%~pI")&Pause
Take a look at the output from for /?, (entered at the Command Prompt), to see the for variable expansion modifiers and what each does.
Batch Style
#Echo Off
For /F "Tokens=* Delims= " %%a In ('Type TXTFileContainingPaths.txt') Do (
For /F "Tokens=* Delims= " %%b In ('Type TXTFileContainingDestinationPaths.txt') Do (
%__APPDIR__%Xcopy.exe "%%~fa" "%%~fb"
)
)
Explanation
It gets the paths from two text files containing source and destination paths using For commands.
Xcopy command copies the files.
For more understanding, open cmd and enter these commands and read carefully:
For /?
Xcopy /?
Type /?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to do a bat script to process file one by one, according to a number in the filename. Here is an example of what I can get :
CDEP_0003.pdf
CIM_0001.pdf
ESCALE_0002.pdf
It's possible to loop and process each of those files in an ordered way using regex or something like this to exclude the number and use it as a loop index ? (0001, 0002, 0003 etc..)
Simply loop through .*0000\.pdf$ to .*9999\.pdf$ using another answer on how to loop through files matching wildcard in batch file
You could
build an array of used numbers by first iterating through the files, or
use a counting loop (for /l) from 10000 to 19999 to have the
leading zeros and take the last 4 digits..
Provided there is only one underscore preceding the number:
#Echo off
Pushd "X:\folder\to\start"
::clear array
for /f "delims==" %%A in ('Set No[ 2^>Nul') do Set "%%A="
:: fill array
for /f "tokens=2delims=_." %%A in (
'Dir /B "*_????.pdf" ^| findstr "^.*_[0-9][0-9][0-9][0-9]\.pdf$"'
) Do Set /A "No[%%A]=%%A"
:: process existing numbers
for /f "tokens=2 delims=[]" %%A in ('Set No[ 2^>Nul') do (
Rem dir "*_%%A.pdf" or whatever you want to do
)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to write a script which will return me list of directories available at the path which I specify.
Could not anybody help. I don't have any idea of cmd commands.
thanks in adavance
Use For Batch file:
for /d %%a in ("C:\Users\") do dir /ad /on /b "%%a"
If User contains 3 folders,
This will print:
Folder1 Folder2 Folder3
If you want all sub-folders list with full path use
for /d %%a in ("C:\Users\") do dir /ad /on /S /b "%%a"
This will print:
C:/Users/Folder1
C:/Users/Folder1/Data
C:/Users/Folder2
C:/Users/Folder3
dir /ad "c:\somepath"
Type dir /? and help.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have few mp3 files that have external site names say www.mymusic.com and that name will be either at the start/end of the mp3 file
Examples:
01 - {www.mymusic.com} Hello Hello.mp3,
02 - {www.mymusic.com} Hai Hai.mp3,
03 - Hello Boys [www.musicworld.in].mp3,
04 - Hello girls [www.musicworld.in].mp3
So I want a (windows) batch script which asks me to enter a name with special characters say "[www.musicworld.in]" and removes it from multiple mp3 files present in a directory.
Also give me an example how to use/run the code.
Please help :)
Thanks in advance
The code works immediately!
#echo off &SETLOCAL enabledelayedexpansion
SET /p "word=enter word to remove: "
IF "%word%"=="" GOTO :EOF
FOR /f "delims=" %%a IN ('dir /a-d /b "*%word%*.mp3"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
)
This is a batch file that can help you - it uses a helper batch file called repl.bat from http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
At the moment it only echos the REN command so remove echo if you are happy with the result. One benefit with this is that ! characters are not an issue as they are with delayed expansion.
#echo off
set /p "var=Enter the text to remove: "
for /f "delims=" %%a in ('dir *.mp3 /b /a-d ^|repl "^(.*)%var%(.*)" "ren \x22$&\x22 \x22$1$2\x22" x ') do echo %%a