How to do a backup with .bat with windows? [closed] - windows

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.

Related

Copy files from multiple directories and create same structure in other separate folder [Windows] [closed]

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 /?

Windows batch script to substitute tabs with 2 spaces [closed]

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 5 years ago.
Improve this question
I need to substitute tab with 2 spaces in all files on a specified path using windows batch script. I see that it is not easy to enter tab character correctly into a batch file. Can anyone help. Thanks
EDIT Corrected output of leading spaces.
The tricky part is to grab the tab char. May be...
#echo off
setlocal EnableDelayedExpansion
rem Grab TAB character
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=7898
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"
rem set desired number of spaces
set "SPACES= "
pushd "your_desired_path"
for /f "delims=" %%f in ('dir /B *.txt') do (
> "output_%%~nxf" (
for /f "tokens=* delims=" %%i in (%%~nxf) do (
set "myData=%%i"
set "myData=!myData:%TAB%=%SPACES%!"
echo !myData!
)
)
echo copy /y "output_%%~nxf" "%%~nxf"
echo del "output_%%~nxf" >NUL 2>NUL
)
popd
endlocal
exit/B
Remove echo from copy and del lines above, if it seems to do the work.

Windows batch script to rename files that have random names? [closed]

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.

get the list of available directories at specified path using batch file(CMD) [closed]

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.

Batch Script: to remove a specific/targeted words that include special characters from multiple filenames [closed]

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

Resources