Batch script that do a backup - batch-file

im a beginner in batch script,and im trying to do a script that ask for a file path,then create a folder(in the current folder)and I want the created folder name to be like "sav-DATE-TIME"(for example,right now the folder would be named: sav-2015-12-07-18-55-00) and then copy the file given by the user filepath in the created folder.So far I did this:
#ECHO OFF
SET /P pathh=Enter the path
SET foldname=sav%DATE%%TIME%
mkdir %foldname%
cd %foldname%
xcopy /s/e %pathh% %cd%
pause
However when I run this,no matter how I enter the path with " or ' or nothing around the path,it always says that the path is incorrect,and also it create a folder with random number(74,56,21...)as the folder name and I dont understand why it wont work properly,wich mean creating the folder with name as said at the begining of question and also saying the path is always incorrect.
thank you!

You need to use quotes when the variables are expanded (used). The first set of quotes below is only to ensure that you don't have any trailing white space chars. Try this:
#ECHO OFF
SET /P pathh=Enter the path
SET "foldname=sav-%DATE%-%TIME:~0,8%
SET "foldname=%foldname::=-%
echo foldname=%foldname%
rem mkdir "%foldname%"
rem cd "%foldname%"
rem xcopy /s/e "%pathh%" "%cd%"
pause
Remove the rem's when it looks good

set foldname=%date:~-4%-%date:~3,2%-%date:~0,2%:%time:~0,2%:%time:~3,2%,%time:~6,2% this should give you 2015-12-09-10:56,17
Then mkdir %foldname%

Related

Recursive rename isn't working at all in batch

So I have been searching in other topics how to rename files and folders in a .bat in a recursive way but it isn't working at all.
My code is:
# echo off
setlocal enabledelayedexpansion
set /p rut="Introduce folder: "
FOR /D /R %%x in ("%rut%"\*) DO (
cd %rut%
echo %cd%
pause
ren .\* "a"
)
exit
But this only renames the files that are on the first folder and not in the rest. Forfile won't work at all with the variables.
What I want to get is how I could rename everything inside the main folder (including subfolders) whatever it is, be it files or folders as "a" to solve the Windows problem of having routes way too long when trying to delete a full structure, that is why I can't use the ".txt" ".whatever" solution.
A powershell solution would be valid too!
Thank you very much
Edit I added a random to see if it was the name that was conflicting but no, it is still not working:
ren .\* "a%RANDOM%"
And renaming them from cmd works the same way, I mean, if I write ren "folder" "whatever" it will change but in the script doesn't work with "*"

Create Batch File that prompts user for File path in DOS 6.22

I am trying to create a .BAT file in DOS 6.22 that will copy the contents of a floppy disk in A: over to C:\ and then set the folder created as a system variable. I tried using something like "SET /P VARIABLE=Enter a path" however DOS will just add "/P VARIABLE" as a variable with the value of "Enter a path" so using the /P isn't an option as /P wasn't a switch in DOS 6.22
I tried using something like a for loop to set a variable to the file however where I hit a speed bump is that I have no idea what the folder is going to be called in drive A:\ as it will change all the time but only ever contain one folder, so basically I am just trying to find a way a way to copy the first directory found in drive A over to C:\ and set that as a system variable. As once the user is done making changes I will have to copy that folder back over to A:\ and overwrite the old files so it can be stored on the network once changes have been made.
I did try experimenting with some If/for statements through a .BAT file but I didn't have much luck with theses, if anyone could point me in the right direction that would be awesome.
At this point I'm probably making this way more complicated than I have to.
something like this should work too:
#echo off
:INPUT.BAT puts what is typed next in environment variable INPUT
set input=
echo INPUT.BAT
echo Type in something and press [Enter]
fc con nul /lb1 /n|date|find " 1: ">temptemp.bat
echo :Loop>>enter.bat
echo if not (%%input%%)==() set input=%%input%% %%5>>enter.bat
echo if (%%input%%)==() set input=%%5>>enter.bat
echo shift>>enter.bat
echo if not (%%5)==() goto Loop>>enter.bat
for %%x in (call del) do %%x temptemp.bat
del enter.bat
echo The string you just entered:
echo %input%
echo has been stored in an environment variable named INPUT
:End
I ended finding a solution after doing some more research from what #Squashman linked. Turns out there was a breakdown in communication and this wasn't even the original issue that the user had (a simple way to copy the files off A:\ and all that)
I used the following.
echo Type "set myvar="name of the folder" replacing name of the folder with
echo the name of the folder containing the files on A:\ example if you were
echo on "example" you would type: set myvar=example
copy con answer.bat
echo Type the words "set myvar=" (don't type the quote marks)
echo and then immediately after the = sign, press Control-Z then enter
call answer.bat
mkdir C:\%myvar%
xcopy A:\%mvar% C:\%myvar%
DEL answer.bat
This is a modified version of a guide I found here. http://www.pement.org/sed/bat_env.htm#4dos
Hopefully this is able to help someone, this isn't pretty by any stretch of the imagination but it worked.
I think this would be easier for your users if they actually need to only copy one directory.
#echo off
if "%1" == "" goto syntax
md C:\%1
xcopy/E A:\%1 C:\%1
goto end
:syntax
echo Please input a directory after %0
:end

Batch file to read the root directory it is in

Is there a batch command that can read a root directory without the entire path?
I want a batch file to tell me if its in D:\ or E:\.
I tried to use:
set mypath=%cd%
#echo %mypath%
Pause
But it just says the exact place it is in rather than just the root.
Here's a few options for you which provide the root directory of the scripts current directory:
Using PushD/Popd
PushD\&Call Set "RootDir=%%CD%%"&PopD
Echo(%RootDir%
Pause
Using a For loop
For %%A In (%CD%) Do Set "RootDir=%%~dA\"
Echo(%RootDir%
Pause
Using variable expansion
Set "RootDir=%CD:~,3%"
Echo(%RootDir%
Pause
Edit
After reading your question again, I decided to add a fourth example. This one unlike the other three provides the root directory of the batch files location.
Set "RootDir=%~d0\"
Echo(%RootDir%
Pause
the directory where the batch file is located could be different from the current directory cmd.exe operates in.
TO get the batch file root path use:
for %%a in ("%0") do echo %%~da
To ger the current directory use
echo "%cd:~0,3%"
And let us not forget the &REM trick.
#ECHO OFF
set "root=%cd:\="&rem %
echo %root%

Set containing folder as variable in batch file

I am trying to get put my containing folder as a set of variable in my batch file. For example i Have
c:\Monday\AM
Monday will be save to variable A while AM will be save to variable B
How can I achieve that?
Once I know that I will create an if else statement base on the name saved on the variable (e.g Monday\AM = 1\2 Tuesday\AM = 2\2 Monday\PM = 1\1)
hope this better explain my question
This will get the directory the batch file resides in (child) and the folder above that (parent).
#echo off
for %%G in ("%~dp0\.") do set child=%%~nxG
for %%G in ("%~dp0\..") do set parent=%%~nxG
echo child=%child%
echo parent=%parent%
I'm quite sure, this is a x-y problem (therefore the request "Try to explain the task"). But if you insist in doing it that way:
#echo off
pushd %~dp0
for %%X in (%cd%) do set _b=%%~nxX
pushd ..
for %%X in (%cd%) do set _a=%%~nxX
popd
popd
echo %_a%, %_b%
This switches into the folder where your batch-file resides (that's %~dp0) and gets the last two elements into variables. When finished, it restores to the original working folder.

batch - differentiating between a file and a path?

#echo off
set /p UserInput= What file would you like to hide?
cd %UserInput%
So I want to make a batch file that when run asks the user for a file, which it will hide in a maze of random folders. Let me explain.
Let's say I type C:\Program Files\Steam\butts.exe
It'd make a new directory in C:\Program Files\Steam
This is where I'm stuck. How do I have it find C:\Program Files\Steam from C:\Program Files\Steam\butts.exe?
Hah! The quickest and hacksiest way would be just to add a \.. to the end. For instance, on my desktop I have a file called weather.xml. If I do this:
dir c:\users\me\Desktop\weather.xml\..
... I end up with a directory listing of my Desktop. :)
So you can accomplish what you need with
cd %UserInput%\..
Otherwise, you could pass the path to a for loop or call :label to a subroutine to end up with %%~dpX. See the last couple of pages of help for and help call for details.
If you want to be even hacksier, instead of requiring the user to enter the path\to\file to set %UserInput%, you can use powershell to launch a File... Open sort of file chooser. See this answer for details.
Try this:
#echo off&setlocal
for %%i in ("C:\Program Files\Steam\butts.exe") do set "steampath=%%~dpi"
:: remove last backslash
echo %steampath:~0,-1%
You can use this batch
#echo off
set /p UserInput=What file would you like to hide?
for %%i in ("%UserInput%") do set path="%%~dpi"
cd %path%

Resources