Batch Script path with space double quote does not work - batch-file

I have this script for mounting iso files
#echo off
set fileiso=%~1
set Exedir="C:\Program Files\OSFMount"
cd /d %Exedir%
osfmount -a -t file -f "%fileiso%" -m #:
if the path is like this:(%fileiso%)
D:\Download\another path with some iso inside\Iso\any.iso
not work
any help please :D

This works for me:
#echo off
set "fileiso=%~1"
set Exedir="C:\Program Files\OSFMount"
pushd "%Exedir%"
osfmount -a -t file -f "%fileiso%" -m #:
popd
pause
Output:
C:\Program Files\OSFMount
Creating device...
Created device 1: G: ->
D:\Old C Drive\Users\williamsonm\Downloads\clonezilla-live-20130314-quantal-i386.iso
Notifying applications...
Done.
Press any key to continue . . .

Related

Batch file console not stopping to display the output

I'm trying to execute a batch file but its console is not stopping for its output.
here's the code:
goto D:
cd postgresql-9.6.0-1-windows-x64-binaries
cd pgsql
cd bin
start pg_ctl start -D D:\pg_data\data
#echo on
echo server started
pause
I am assuming it is on the D: drive. use cd /d see cd /? for the reason why.
#echo off
cd /d "D:\postgresql-9.6.0-1-windows-x64-binaries\pgsql\bin"
start "" "pg_ctl" start -D "D:\pg_data\data"
echo server started
pause
change to start "" /wait if you want to wait for the service to start before you echo it has been started.

How to use rsync instead of move in for loop in batch

In every subfolder in the path U:\0012* I create an example folder and move the files from appropriate subfolder to example folder.
FOR /d %%A IN (U:\0012\*) DO mkdir %%~A\example & move %%~A\*.* %%~A\example\
What I want to try is to use rsync command instead of move
FOR /d %%A IN (U:\0012\*) DO
C:\cygwin\bin\rsync.exe -av -h --progress --checksum "/cygdrive/U/0012/%%A/*.*/" "/cygdrive/U/0012/%%A/EXAMPLE/"
The script is closing itself with a syntax error. Is it possible to use rsync in for loop?
I think you are having a clash of "Windows World" and "Unix World".
What you are trying with rsync, which is natural Unix tool, is to copy using a batch file path and put it into rsync path. This will not work.
I'll dissect your code:
In this part you are still in the "Windows world" you are using FOR to loop via the U:\0012\* path in a windows way FOR /d %%A IN (U:\0012\*) DO.
You have tried to "help" rsync with unix style path /cygdrive/U/0012/, but the variable %%A contains the windows style path!
C:\cygwin\bin\rsync.exe -av -h --progress --checksum "/cygdrive/U/0012/%%A/*.*/" "/cygdrive/U/0012/%%A/EXAMPLE/"
The error you are getting is probably something like:
The source and destination cannot both be remote. rsync error: syntax
or usage error (code 1) at main.c(1292) [Receiver=3.1.2]
How to solve such an error?
First is to say I'm using MSYS environment for linux tools. I have tested it on my local files.
In batch files you could do it the following way:
#echo off
FOR /d %%A IN (U:\0012\*) DO (
SET "copy_path=%%A"
C:\msys64\usr\bin\cygpath.exe -u %copy_path% > tmp_file.tmp
SET /P unix_path=<tmp_file.tmp
DEL tmp_file.tmp
ECHO "%unix_path%"
C:\msys64\usr\bin\rsync.exe -av -h --progress --checksum --remove-source-files "%unix_path%" "%unix_path%/EXAMPLE/"
)
)
The script takes the Window path C:\prg\PowerShell\test\SO\test1\* and then it uses a cygpath.exe to convert the path to a unix style path and saves it to temporary file (tmp_file.tmp). Then it reads from temporary file the path into the unix variable %unix_path% which is then used in the rsync.exe.
Note: As somebody already linked how to move file with rsync. I'll just state the option. You have to add --remove-source-files, which deletes files after transfer.
** Edit **
I think I now understand what you mean. Now rsync should really behave like move. Only difference is that if directory example exists it is copied with the file. (if it does not it is created and files moved there) - I think that was original intentions when looking at mkdir && move.
What I have done?
ENABLEDELAYEDEXPANSION to correctly read the variables from file (before it was only the last read)
"!unix_path!/" the last backslash is really important for rsync to behave correctly.
I have tested the script on my computer and for me it works as it should.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%A IN (C:\prg\t\*) DO (
ECHO %%A
SET "copy_path=%%A"
C:\msys64\usr\bin\cygpath.exe -u !copy_path! > tmp_file.tmp
SET /P unix_path=<tmp_file.tmp
DEL tmp_file.tmp
ECHO "!unix_path!"
ECHO C:\msys64\usr\bin\rsync.exe -av --progress --checksum --remove-source-files "!unix_path!/" "!unix_path!/example/"
C:\msys64\usr\bin\rsync.exe -av --progress --checksum --remove-source-files "!unix_path!/" "!unix_path!/example/"
)
** Second Edit ** Empty directories are not deleted with rsync
I do not recommend using --remove-files && --delete options during one operation due to the fact the rsync can fail. You can read more here superuser.com or unix stack exchange or even server fault.
I would simply go with already written solution: How to delete empty folders using windows command prompt? (I'm talking the solution (all credits to author) that takes in also directories with spaces):
for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"
The final script would then look like this:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%A IN (C:\prg\t\*) DO (
ECHO %%A
SET "copy_path=%%A"
C:\msys64\usr\bin\cygpath.exe -u !copy_path! > tmp_file.tmp
SET /P unix_path=<tmp_file.tmp
DEL tmp_file.tmp
ECHO "!unix_path!"
ECHO C:\msys64\usr\bin\rsync.exe -av --progress --checksum --remove-source-files "!unix_path!/" "!unix_path!/example/"
C:\msys64\usr\bin\rsync.exe -av --progress --checksum --remove-source-files "!unix_path!/" "!unix_path!/example/"
)
REM To delete empty directories - the windows way
REM save current directory to stack
pushd
cd C:\prg\t
REM Will delete all empty directories (subdirectories) at current path
for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"
REM return to saved directory
popd
rsync can run inside the for loop. Below is the sample code for moving all files from a directory to "EXAMPLE" directory in same directory.
#!/bin/sh
for d in ~/Desktop/a/*; do
rsync -rav -h --progress --checksum $d/*.* $d/EXAMPLE/
done
Save the above in a .sh file and run the file. Here the above code will go through all subdirectories in ~/Desktop/a/ directory and will create EXAMPLE directory in each subdirectory and copy all files to EXAMPLE directory.

batch write reg file fails with "The system cannot find the file specified"

I am trying to create reg file inside bat file with the following lines:
echo Windows Registry Editor Version 5.00 >tst222.reg
echo [HKEY_USERS\S-1-5-21-3297735606-1256148830-2878094939-1000\Software\GoldNET\Automate]>>tst222.reg
echo "ExecApplication4"="c:\\AttPlus\\go.bat,,"\Arj a -y <MivzaqBakDataPath>{MivIx}.arj <MivzaqMsgsDataPath>*.ms*"\,,"\<BankExplore> -I <MivzaqMsgsDataPath>*.msq -E"\">>tst222.reg
pause
The first line and the second go well but the third fails with the error:
The system cannot find the file specified
what am I doing wrong in there and how can I solve this?
the exact line should appear like this in the reg file:
"ExecApplication4"="c:\\AttPlus\\go.bat,,\"Arj a -y <MivzaqBakDataPath>{MivIx}.arj <MivzaqMsgsDataPath>*.ms*\",,\"<BankExplore> -I <MivzaqMsgsDataPath>*.msq -E\""
P.S
i tried to do this threw reg add on the bat file and got similar error
Try it with ^> and ^< instead, to 'escape' the > and < characters causing the problem (redirecting input/output). Line 3 :
echo "ExecApplication4"="c:\\AttPlus\\go.bat,,"\Arj a -y ^<MivzaqBakDataPath^>{MivIx}.arj ^<MivzaqMsgsDataPath^>*.ms*"\,,"\^<BankExplore^> -I ^<MivzaqMsgsDataPath^>*.msq -E"\">>tst222.reg
Example echo in cmd window, and output :
C:\>echo "ExecApplication4"="c:\\AttPlus\\go.bat,,"\Arj a -y ^<MivzaqBakDataPath
^>{MivIx}.arj ^<MivzaqMsgsDataPath^>*.ms*"\,,"\^<BankExplore^> -I ^<MivzaqMsgsDa
taPath^>*.msq -E"\"
"ExecApplication4"="c:\\AttPlus\\go.bat,,"\Arj a -y <MivzaqBakDataPath>{MivIx}.a
rj <MivzaqMsgsDataPath>*.ms*"\,,"\<BankExplore> -I <MivzaqMsgsDataPath>*.msq -E"
\"

Upload file with Batch program

Good afternoon all,
I am having an bit of trouble with the following Batch file.
#echo off
set /p Pass=Enter your password:
ncftp <<EOF
open -u thomas -p %Pass% MyHost.com
cd "Program"
lcd "../Program"
put -R *
bye
EOF
It returns after the password the following error
<< not expected at this time
I have this script from somewhere on the internet. And if he does it it works like an charm.
What am i doing wrong or what must i do i have searched the internet with << but i didnt get any search results on that matter.
With kind regards,
Thomas de Vries
This is the problem in your script.
ncftp <<EOF
<<EOF is a syntax error and <EOF would be redirecting a file called EOF in the same folder, into the ncftp executable.
It would seem that your source material is not quite debugged.
#echo off
set /p Pass=Enter your password:
(
echo open -u thomas -p %Pass% MyHost.com
echo cd "Program"
echo lcd "../Program"
echo put -R *
echo bye
) | ncftp

How to batch convert .sph files to .wav with sox

I have a directory with many folders containing hundreds of .SPH files. I need to convert all .SPH files into .wav format. I have adopted the following code:
cd %~dp0
mkdir converted
FOR %%A IN (%*) DO sox -t raw -s -2 -r 22050 -c 2 %%A "converted/%%~nA.wav"
pause
However, it doesn't do anything on Windows 7. When I try the code on CMD inside a folder where some of .SPH are:
sox *.SPH output.wav
It embeds all *.SPH into output.wav file, which is not what I want. I need name1.SPH to name1.wav, name2.SPH to name2.wav
Please help.
For Linux consider this:
for f in *.SPH; do sox -t sph "$f" -b 16 -t wav "${f%.*}.wav"; done
In Windows 7, I need to provide the entire path for the destination files, and the mdkir command got a permission error even though I was running as Administrator, so pre-make your destination directory and delete the mkdir command, and then your SOX command should look similar to this (with your own flags based on the conversion or edit you are trying to accomplish):
FOR %%A IN (%*) DO sox -t raw -e mu-law -c 1 -r 8000 %%A c:\converted\%%~nA.wav
The cmd file should not be in the folder where the files are, it should be in the folder where SOX is installed, and then drag the files you want converted onto the cmd (or bat) file.
for %%a in (*.sph) do sox "%%~a" "%%~na.wav"
Make a .bat file in your sox directory with the contents
cd %~dp0
for %%a in (*.sph) do sox "%%~a" "%%~na.wav"
pause
Select and drag the files that you want to convert onto this .bat file and the .wav files with the same names as the .sph files will appear in the same folder as the .sph files.

Resources