two input variables in a simple bat file - windows

I am trying to write my first bat file. Sorry......
I need to input a source directory (dir) for another bat file to find test files and a destination directory (log) for the bat file to output its results.
The bat file that does the processing is called fits.bat.
-i Indicates that a file or directory to process will follow
-o Directs the FITS output to a file
I have this:
#echo off
cd c:\program files\fits\fits-0.8.0
SET /P dir=enter directory of source files
SET /P log=enter directory for log files
fits.bat -i %dir -o %log
pause
But I get no output at all.
If I type individual commands in command prompt window , it works and files in dir folder are correctly read and output is sent to the log folder

try this....
#echo off
cd c:\program files\fits\fits-0.8.0
set /p dir=enter directory of source files
set /p log=echo enter directory for log files
fits.bat -i %dir% -o %log%
pause
when you call variables you need a % in front and back
%VAR%

You need to surround your two variables in percent signs:
#echo off
cd c:\program files\fits\fits-0.8.0
SET /P dir=enter directory of source files:
SET /P log=enter directory for log files:
fits.bat -i %dir% -o %log%
pause

Related

Change batch file directory in system32 to CMD command execution point

So basically I have a .bat that is inside my System32 folder.
This batch file accepts a parameter input, this input is a file.
I wanted it to be that I could open my Command Prompt, and for example do
batchfile text.txt
And it would pass test.txt into batchfile.bat. Obviously for my terminal to do this it needs to be in System32.
That is where my issue is. Because the batch file is in System32, when executing the command, it changes my directory to System32.
However the parameter I give it is a file. And when the command is executed and it changes the directory to System32, obviously it can no longer access the file.
How can I get around this?
Before you change directories, expand the filename to its fully-qualified filename and then reference it that way:
#echo off
for %%F in (%1) do set USR_REQFNM=%%~dpnxF
C:
cd \Windows\System32
echo The requested file is "%USR_REQFNM%"
goto ENDIT
REM Cleanup
:ENDIT
set USR_REQFNM=

Converting multiple PDF files to Text files in subdirectory using Ghostscript in Windows

OS: Windows 10 Professional
I'd like to convert multiple PDF files to text files in main and sub-directories using GhostScript on Windows command line. Here's my command below:
gswin64c -sDEVICE=txtwrite -o test1.txt "test1.pdf"
This code converts "test1.pdf" to "test1.txt" using Ghostscript but I would like to perform the following:
Look for all pdf files in main directory and all sub-directories
Execute Ghostscript on all PDF files
Give the same file name to the output text file. (test1.pdf -> test1.txt)
I appreciate your time and consideration on this!
Build a batch or .cmd file like this:
#echo off
REM Replace these with your actual location
D:
cd "\Main Directory"
for /R %%F in (*.pdf) do call :DOPDF "%%F"
goto ENDIT
:DOPDF
for %%X in (%1) do set PDF_TXTFNM=%%~dpnX.txt
gswin64c -sDEVICE=txtwrite -o "%PDF_TXTFNM%" %1
goto :EOF
REM Clean up
:ENDIT
set PDF_TXTFNM=
EDIT TO ADD:
Side Note: If you do not wish to change your working directory to the main directory, be aware that for /R %%F in ("D:\Main Directory\*.pdf") ... will only work if there actually is a .pdf file in D:\Main Directory. The two main workarounds are to make it the current working directory (the solution chosen in my example) or to force a dummy .pdf file to exist in that directory and then choose to not process it in the subroutine using an IF statement. Holler if you need an example of that latter technique.

Windows Script to copy or update a jar file is not working

I have prepared the below script to copy "sigma 1.jar file from source to destination. But the entire folder is getting copied instead of just the sigma 1.jar file. All I want is that jar file to be copied to destination. Nothing else should be copied.
I have tried the /E option & getting the same result.
Below is the script
#mkdir Z:\backup20160812
#xcopy "Z:\Testing\A 1\Sigma 1.jar" "Z:\backup20160812\A 1\Sigma 1.jar" /H
#echo -------backup successfully!-------
#pause
Rename the files and folders so there are no spaces (ex: A1 instead of A 1)
then try
#mkdir Z:\backup20160812
#xcopy "Z:\Testing\A1\Sigma1.jar" "Z:\backup20160812\A1\Sigma1.jar" /H
#echo -------backup successfully!-------
#pause

Use embedded file from .exe

I made an exe that has embedded files in it like a portable 7zip (7za.exe) and I want to call to it in the batch script that I am compiling into an exe but when I do it just gives me "7za.exe" is not recognized as an internal or external command. If I left anything out just ask.
(Sorry if this is an easy fix I am just messing around with some basic code)
This is the code I am working with and exe is in releases tab.
https://github.com/iamtis/mass-extract
Let us look on batch file with some additional lines at top:
#echo off
echo Current working directory is: %CD%
echo Directory of batch file is: %~dp0
pause
echo Files in current working directory:
dir /A-D /B
pause
echo Files in directory of batch file:
dir /A-D /B "%~dp0"
pause
I suppose that the current working directory is not equal the directory of the batch file and the tools are in the directory of the batch file. I suppose the batch file directory is a subdirectory with random name in %TEMP%.
So what you most likely need is:
#echo off
set "ToolPath=%~dp0"
if not exist "%CD%\archive\*" md "%CD%\archive"
"%ToolPath%7za.exe" x "%CD%\*.zip" "%CD%\archive\"
"%ToolPath%7za.exe" x "%CD%\*.7z" "%CD%\archive\"
"%ToolPath%unrar.exe" x "%CD%\*.rar" "%CD%\archive\"
"%ToolPath%7za.exe" a -mx9 archive.7z "%CD%\archive\"
rd /S /Q "%CD%\archive"
set "ToolPath="

How to verify if a file exists in a batch file?

I have to create a .BAT file that does this:
If C:\myprogram\sync\data.handler exists, exit;
If C:\myprogram\html\data.sql does not exist, exit;
In C:\myprogram\sync\ delete all files and folders except (test, test3 and test2)
Copy C:\myprogram\html\data.sql to C:\myprogram\sync\
Call other batch file with option sync.bat myprogram.ini.
If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.
You can use IF EXIST to check for a file:
IF EXIST "filename" (
REM Do one thing
) ELSE (
REM Do another thing
)
If you do not need an "else", you can do something like this:
set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=
Here's a working example of searching for a file or a folder:
REM setup
echo "some text" > filename
mkdir "foldername"
REM finds file
IF EXIST "filename" (
ECHO file filename exists
) ELSE (
ECHO file filename does not exist
)
REM does not find file
IF EXIST "filename2.txt" (
ECHO file filename2.txt exists
) ELSE (
ECHO file filename2.txt does not exist
)
REM folders must have a trailing backslash
REM finds folder
IF EXIST "foldername\" (
ECHO folder foldername exists
) ELSE (
ECHO folder foldername does not exist
)
REM does not find folder
IF EXIST "filename\" (
ECHO folder filename exists
) ELSE (
ECHO folder filename does not exist
)
Here is a good example on how to do a command if a file does or does not exist:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"
Use the XCOPY command:
xcopy "C:\myprogram\html\data.sql" /c /d /h /e /i /y "C:\myprogram\sync\"
I will explain what the /c /d /h /e /i /y means:
/C Continues copying even if errors occur.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/H Copies hidden and system files also.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
`To see all the commands type`xcopy /? in cmd
Call other batch file with option sync.bat myprogram.ini.
I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like
Path/sync.bat
Path/myprogram.ini
If it was in the Bash environment it was easy for me, but I do not
know how to test if a file or folder exists and if it is a file or
folder.
You are using a batch file. You mentioned earlier you have to create a .bat file to use this:
I have to create a .BAT file that does this:
Type IF /? to get help about if, it clearly explains how to use IF EXIST.
To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one
Finally copying just means calling COPY and calling another bat file can be done like this:
MYOTHERBATFILE.BAT sync.bat myprogram.ini

Resources