I want to zip a project folder each month, programmed in a task scheduler executing a .bat file with the following script:
#echo off
set backuptime=%date:~4,2%-%date:~7,2%-%date:~10,4%
"C:\Program Files\7-Zip\7z.exe" a -tzip "D:\backups\projects\%1\%backuptime%.backup.zip" "D:\xampp\htdocs\%1"
echo Done!
%1 is the name of the project as parameter.
When I execute the bat as a Windows command (cmd), works fine generating a file: 17052022.backup.zip, but when is executed via Win task scheduler, the result is this:
Why the day and the month are a new folders?
Solution
Thanks for the suggestion. I have generated the zip with the date name from the O.S. configuration and not from my own user:
#echo off
for /f %%# in ('wmic os get localdatetime^|findstr .') do if "%%#" neq "" set date=%%#
set date=%date:~,4%%date:~4,2%%date:~6,2%
"C:\Program Files\7-Zip\7z.exe" a -tzip "D:\backups\projects\%1\%date%.zip" "D:\xampp\htdocs\%1"
echo Done!
Related
I'm trying to feed the file name dynamically to a command through windows batch script. Here's my code,
#echo off
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml %%I)
pause
Basically, I want to get all the .java files from any given directory and feed those java files to another command which will run for each file. In place of %%I I am trying to provide the java file dynamically. But it says java command not recognized.
If I use java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml path/filename.java alone in a batch file. It works fine.
I have tried the following also,
#echo off
set i=-1
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (
set /a i=!i!+1
set filenames[!i!]=%%I
)
set lastindex=!i!
for /L %%f in (0,1,!lastindex!) do (
java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml !names[%%f]!
)
pause
But again it says java command not recognized.
What am doing wrong here? Why I can not pass the .java file names dynamically?
Your named your variable with project path "path", and it's bad, because as soon as you do
set /p path="Enter the project path: "
you overwrite the system "path" and then Windows cannot find your "java.exe".
Rename it to project_path:
set /p project_path="Enter the project path: "
for /R "%project_path%" %%I in .......
For example, I have a simple batch script get_path.bat
#echo off
echo C:\Software\dt
Also, I have another simple batch script switch_dir.bat
#echo off
get_path.bat > target.tmp
set /p TARGET=<target.tmp
cd %TARGET%
Now, what I want to accomplish is that when I invoke in cmd.exe the batch file switch_dir.bat, my current working directory changes to C:\Software\dt.
As of now, the scripts work but they run in a process spawned from cmd.exe so my current working directory stays the same. What is missing? Basically, we need Unix-like source or . here.
Well, there are many possible solutions:
Start your script with cmd /c:
All you have to write in cmd is:
cmd /c switch_dir.bat
Using popd/pushd in your batch file:
In your switch_dir.bat add:
#echo off
pushd dir\you\want\to\remain\
get_path.bat > target.tmp
set /p TARGET=<target.tmp
cd %TARGET%
rem [code...]
popd
An additional note: A better way to find directory specified in get_path.bat is to use a for /f loop like this:
#echo off
pushd dir\you\want\to\remain\
for /f "delims=" %%A IN ('get_path.bat') do set TARGET=%%A
cd %TARGET%
rem [code...]
popd
i have a batch file scripted which makes a backup of some of my mysql database tables and saves them in a new created folder with this pattern as name: "date_time" so as example "23.06.2016_1050".
So i have another batch file which imports every *.sql file in a specific folder back to my database.
What i want to do is, writing a batch which automaticly selects the latest folder and then runs my part of code in that.
Here is my batch code:
cd c:\server\backup\character_data
FOR %%X IN (*.sql) DO ECHO Importing %%X & "C:\Program Files\MariaDB 10.1\bin\mysql" dspdb -h localhost -u root -p 123456789 < %%X
So i need to cd a %variable% which contains the name of the folder with the latest date and time pattern as its name.
So this:
cd c:\server\backup\character_data\%variable%
would be:
cd c:\server\backup\character_data\23.06.2016_1050
Does anyone knows how to get the desired folder name as variable somehow?
I really hope that i explained it good enough and someone can help me with that.
Thx in advance! :)
This will get the most recent folder in c:\server\backup\character_data with the full file path, so no need to do cd c:\server\backup\character_data\%variable%, instead it would be cd %a%
If you don't want the full directory of the folder then remove the /s from the dir command
#echo off
cd /d "c:\server\backup\character_data"
FOR /F "delims=" %%i IN ('dir /b /s /ad-h /t:c /od') DO SET a=%%i
::Do what you want with your folder, call it with %a%
I have a problem when calling the .exe file through a batch script. Below is my batch script code:
#echo off
for /F "tokens=* delims=" %%A in (MyFile.csv) do start " "diffapicmdline.exe /lhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 %%A" /rhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 Prjct %%A" /t job /ot html /ol "C:\compare_output_H_S_Component.html""
So for each row in MyFile.csv file diffapicmdline.exe should be called every time.
But the problem is that it is not recognizing the parameters after .exe. Do we have to escape characters or find other ways to tell the batch script to run .exe successfully?
Error I am receiving here:
Windows can't find 'diffapicmdline.exe /lhscd "/d=localhost:9080'.
Make sure you typed the name correctly, and then try again.
Would this work better, calling the exe in-line instead of launching a new CMD process?
#echo off
for /F "tokens=* delims=" %%A in (MyFile.csv) do diffapicmdline.exe /lhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 %%A" /rhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 Prjct %%A" /t job /ot html /ol "C:\compare_output_H_S_Component.html"
So I've been trying to create a batch file for a piece of software called DiscEX the software requires command line use from cmd.exe windows xp or higher the way it's initiated is like this discex (any arguments needed) location of iso file.
Now I can get the software to run using the batch file but I can't seem to figure out how to copy the target location of a file that was dragged onto it to open the batch file up
Here is what the batch file in notepad looks like.
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex
pause
also I need to be able to start in the working directory of a portable hard drive.
All you need to do is
C:\windows\system32\discex "%1"
to get a file path argument passed into the batch
If the batch file is in the working directory already, put
cd /d %~dp0 in the batch after #echo off
If you want to determine what drive is the external usb drive, use
#echo off
setlocal
set wmi='wmic logicaldisk where "volumeserialnumber='32A78F3B'" get caption'
for /f "skip=1 delims=" %%A in (%wmi%) do (
for /f "tokens=1 delims=:" %%B in ("%%A") do (set drive=%%B)
)
echo %drive%
where volumeserialnumber is the output from vol [driveletter of USB drive:] with the - removed.
When you drag a file on a batch file, the full file path is available in the first argument (%1) of the batch file. If you need this argument to be fed to the discex application as its first argument, you can do:
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex %1
pause