Batch - Why is 7zip command working improperly? - batch-file

Here is my code:
net use v: \\kimezgls-rez\arxiv_mppi
net use w: \\###.###.#.##\Protokoliem\KIMEZGLS-REZ_mppi
cls
cd V:\
if not exist "V:\01_2016" mkdir "V:\01_2016"
pause
ECHO Folder is Created!
ECHO Moving Files...
MOVE "V:\??_01" "V:\01_2016"
ECHO Creating Backup!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for /d %%X in (01_2016) do "C:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\"
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ECHO Please wait few seconds...
ECHO Backup is Created!
ECHO Sending Backup to ###.###.#.##\Protokoliem\KIMEZGLS-REZ_mppi
MOVE "V:\01_2016.7z" "w:\"
pause
cls
pause
GOTO MENU
I am trying to make script, that will Create directory, name it 01_2016, and put all files, where name ends with _01 in it. Then Archive folder 01_2016 and send to Another Network folder.
Everything went well to the point, where script started to Archive folder 01_2016, but couldn't.
Scanning the drive:
WARNING: The system cannot find the file specified.
01_2016
0 files, 0 bytes
Creating archive: 01_2016.7z
Items to compress: 0
Files read from disk: 0
Archive size: 32 bytes (1 KiB)
Scan WARNINGS for files and folders:
01_2016 : The system cannot find the file specified
----------------
Scan WARNINGS: 1
For some reason it saves empty .zip folder right on my Desktop. While anylyzing whole code, I came to conclusion, that this command is only thing that doesn't want to let me successfully run script.
Thanks for any help

I guess the problem is that CD V:\ is not enough. If you are on C:\ CD V:\ won't move the scope to V:. To achieve this you have to add the /d switch to the CD command:
...
cd /d V:\
...

Related

Copy command has stopped working after merging batch files

I inherited a set of batch files.
Bat1 called bat2 which called bat3.
Bat3 created an xml, which bat2 used to create an html and bat1 used that to create a pdf.
Finally bat1 copied the pdf to an output directory.
I've recently joined the three batch files into one and everything works exactly the same up to the final copy command, which fails stating the pdf cannot be found.
#echo off
pushd %~dp0
set sourceFile=%1
set xslt=%2
set outputFile=%3
for %%i in (%sourceFile%) do (
set documentName=%%~ni
)
::BATCH FILE 2 CALLED
::BATCH FILE 3 CALLED
echo Processing source file compose
java -cp lib\saxon9.jar; net.sf.saxon.Transform -s:%sourceFile% -xsl:conversion\compose.xsl -o:compose.xml
echo Processing source file catalog
java -cp lib\saxon9.jar; -s:compose.xml -xsl:conversion\catalog.xsl -o:catalog.xml
::END OF BATCH FILE 3
echo Processing source file to HTML
java -cp lib\saxon9.jar; net.sf.saxon.Transform -s:catalog.xml -xsl:conversion\html5.xsl -o:index.html
echo Bundling publication
mkdir %documentName%
xcopy /y "conversion\webapp\*" %documentName%\ /o /x /e /h /k
move index.html %documentName%\
copy source graphics into web application img res dir
for %%f in ("*.jpg" "*.png" "*.gif" "*.svg" "*.psd" "*.ai" "*.pdf") do copy "%%f" %documentName%\asset\img\
::END OF BATCH FILE 2
echo Running AHF
AHFCmd -extlevel 4 -d %documentName%\index.html -o %documentName%.pdf -f HTML -x 4 -i tool\AHF\AHFSettings(x64).xml -stdout
::EVERYTHING WORKS FINE TO HERE; %documentName%.pdf is created just as it was previously.
echo Moving %documentName%.pdf
copy %documentName%.pdf %outputFile% ::Error - The system cannot find the file specified.
popd
The line copy %documentName%.pdf %outputFile% hasn't changed. I have tried using CALL in front of the AHFCmd and JAVA commands but that makes no difference. I have tried dir /b /a-d just before the copy command and it lists the file I want to move in the current directory. What am I doing wrong?

CMD/FTP to create folder using to today date & connect ftp download into created folder

I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B

Batch file check for anything in folder

I have been tinkering with some batch files to clean up some of my folders and I am stuck.
What I am trying to do is check the directory Installers for any files or folders and then either :goto empty or :goto notempty .
I have spent ages searching for a solution but everything that I have found is to either check if only files exist in a directory or check if a specified folder exists in a directory.
EDIT: This is what I have so far.
#echo off
echo Beginning File Cleanup
echo Installers Start
"C:\Program Files\WinRAR\rar.exe" a -r -df "Installers.rar" Installers
echo Installers Done
echo old Start
"C:\Program Files\WinRAR\rar.exe" a -r -df "old.rar" old
echo old Done
mkdir Installers
mkdir old
pause
The code above works but I only want it to run the rar.exe bit if the folder is empty hence to :goto requirement
Thanks
I must be missing something, but I didn't saw how does you current code relate to what you asked...
You can do what you want with the following line
dir /b "path/to/Installers" | findstr "^" >nul && (echo Folder not empty) || (echo Folder is empty)
Just see what happens at the echo parts and replace for the command you want.
Cheers.

How to create a text file inside a folder on the desktop using batch

I am trying to get a batch file to create a folder on the desktop with a text file inside of it. Every-time i try to run this line of code it gives my the error that "The filename, directory name, or volume label syntax is incorrect."
echo ========================
::CREATE FILES START
cd /d C:
md Title
echo.>"C:\Users\%USERACCOUNT%\Desktop\Example\example.txt"
::CREATE FILES END
echo Done!
pause >nul
Your code is changing to drive C, then creating GeoHunt2015 in root. Then you try to echo the file into non-existent folder on desktop, hence the error.
This assumes your %userprofile% is "c:\users\name"
md "%USERPROFILE%\Desktop\GeoHunt2015"
echo.>"%USERPROFILE%\Desktop\GeoHunt2015\Mission_Instuctions.txt"
or you can cd to desktop
echo ========================
:: CREATE FILES START
cd /d "%USERPROFILE%\Desktop\"
md GeoHunt2015
echo. >"GeoHunt2015\Mission_Instructions.txt"
:: CREATE FILES END
echo Done!
pause >nul
Is %USERACCOUNT% defined?
Is the echo actually causing the issue?
Try commenting out stuff until you are sure that the echo is causing the syntax error.
A couple things I can see. You're switching to the C: directory, then making the GeoHunt2015 folder, but then attempting to echo into the GeoHunt2015 folder on your desktop.
Try this echo instead:
echo.>"C:\GeoHunt2015\Mission_Instructions.txt"
Try using
mkdir "C:\%USERPROFILE%\Desktop\GeoHunt2015"
echo.>"C:\%USERPROFILE%\Desktop\GeoHunt2015\Mission_Instuctions.txt"
as you had in your original version of the question.

windows batch script to unzip a file

I am trying to create a batch file that gets a zipped folder for that particular date (v_date) from the sftp site and then unzip them. The zip file contains five files that are text documents. I have written batch scripts that successfully get the zip file from the remote site and save a copy of it on the local drive. I need to incorporate the unzip part in to my script.
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
echo option batch continue>FTP_File_Get.txt
echo option confirm off>>FTP_File_Get.txt
echo open Target>>FTP_File_Get.txt
echo lcd "M:\Development\Data History\File" >> FTP_File_Get.txt
echo cd /Export/File >> FTP_File_Get.txt
echo get /Export/File/Filename_%v_date%.zip "M:\Development\DataHistory\Filename_%v_date%.zip">>FTP_File_Get.txt
echo exit>>FTP_File_Get.txt
M:\temp\apps\WinSCP\winscp.com/script="M:\Development\SFTPBatchFiles\FTP_File_Get.txt"
del FTP_File_Get.txt
This is my code to UNZIP:
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
cd "M:\Development\Data History\"
::SET v_file="M:\Development\Data History\Filename_%v_date%.zip"
::unzip -o %v_file%
"C:\Program Files\7-Zip\7z.exe" e "Filename_%v_date%.zip"
I need to move the extracted files (6 Files) into their respective folders, Any help is greatly appreciated
To unzip the files you can use this command line:
"C:\Program Files\7-Zip\7z.exe" e "filename.zip"
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Use the above code by saving it to .bat file extension
Remember %userprofile% is for the directory, %programfiles% for the program files set as a variable in windows
hope that helps

Resources