I'm trying to write a batch file to act as an interface between Source-Insight, and Git running on a Linux server, but I'm running into an issue where the set /p does not seem to be working as advertised.
The batch file is supposed to run a linux script (via plink), which will check out the appropriate files into two directories, and then invoke Beyond Compare to compare the directories (note, these are on an sftp mounted drive so that dos can see them). The directory names are dynamic, so I need the batch file to read the generated directory name from a file before passing it to Beyond Compare. I can't seem to get this working...
I have the following lines in my batch script:
#echo on
plink server1234 -l %user% -i %ppk_file% "cd %root%; ~/bin/compare_git_all.sh --debug --ref"
echo "set /p dir=<%dosroot%\.comparegit.dosdir"
set /p dir=<%dosroot%\.comparegit.dosdir
echo dir="%dir%" (from %dosroot%\.comparegit.dosdir)
"C:\Program Files\Beyond Compare 4\BCompare.exe" %dir%.refpt %dir%
#echo off
My output ends up being:
"set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir"
dir="" (from z:\builddir\pd2\wt1\.comparegit.dosdir)
So first issue (annoyance really), is that #echo on is not causing the commands to be echoed (which according to the pages I've google it's supposed to do...)
But what's killing me is that %dir% seems to be blank. I've verified that the file contains the data I am looking for:
C:\>more z:\builddir\pd2\wt1\.comparegit.dosdir
z:\builddir\pd2\wt1\.comparegit.zmP8BK
if I run the same command from a Command Prompt, I get:
C:\>set dir=blank
C:\>echo %dir%
blank
C:\>set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir
C:\>echo %dir%
z:\builddir\pd2\wt1\.comparegit.zmP8BK
So, I'm missing something, but I'm not sure what it is. Any help would be appreciated. (note, if it makes any difference, the batch file is being invoked from a keymapping within Source Insight)
Related
I am writing a batch script that installs some applications from MSI files from the same folder.
When I write those commands in command prompt window, all is fine and all the commands work properly.
But when I write them into the batch script, suddenly most of the commands such as XCOPY, msiexec, DISM result in an error message like:
'XCOPY' is not recognized as an internal or external command, operable program or batch file.
After googling it for a while, I saw a lot of comments related to the environment variable PATH which should contain C:\Windows\system32 and I made sure its included in the PATH. Also found a lot of answers about writing the full path which I already tried and it didn't work.
I'm working on Windows server 2012.
This is the code of my batch file:
#echo off
set path=C:\ rem default path
rem get the path as parameter to the script:
set argC=0
for %%x in (%*) do Set /A argC+=1
if %argC% gtr 0 (set path=%1%)
IF %ERRORLEVEL% NEQ 0 (
echo %me%: something went wrong with input directory
)
echo Destenation: %path%
SETLOCAL ENABLEEXTENSIONS
SET me=%~n0
SET parent=%~dp0
echo %me%: starting installation of Python 2.7 64bit and Apache 64 bit
REM install .net 3.5
DISM /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:installationMediaDrive:\sources\sxs
msiexec /i ".\py\python-2.7.amd64.msi" TARGETDIR=%path%/Python27 /passive /norestart ADDLOCAL=ALL
mkdir %path%\Apache24
XCOPY /e /Q ".\Apache24" %path%\Apache24
It looks like the batch file should support an optionally specified path to installation directory as first parameter. The code used to check for existence of this optional folder path is very confusing. There are definitely easier methods to check for an optional parameter as it can be seen below.
The main problem is redefining environment variable PATH which results in standard console applications of Windows stored in directory %SystemRoot\System32 and other standard Windows directories are not found anymore by command interpreter cmd.exe on execution of the batch file.
In general it is required to specify an application to execute with full path, file name and file extension enclosed in double quotes in case of this complete file specification string contains a space character or one of these characters &()[]{}^=;!'+,`~ as explained in last paragraph on last output help page on running in a command prompt window cmd /?.
But mainly for making it easier for human users to execute manually applications and scripts from within a command prompt window, the Windows command interpreter can also find the application or script to run by itself if specified without path and without file extension.
So if a user enters just xcopy or a batch file contains just xcopy, the Windows command interpreter searches for a file matching the pattern xcopy.* which has a file extension as defined in semicolon separated list of environment variable PATHEXT first in current directory and if no suitable file found next in all directories in semicolon separated list of environment variable PATH.
There are 3 environment variables PATH:
The system PATH as stored in Windows registry under:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
The folder paths in system PATH are used by default for all processes independent on used account.
The user PATH as stored in Windows registry under:
HKEY_LOCAL_MACHINE\Environment
The folder paths in user PATH are used by default only for all processes running using the account on which the user PATH was set.
The local PATH just hold in memory in currently active environment of running process.
The system and the user PATH are concatenated by Windows to a single local PATH for processes.
Every time a process starts a new process like Windows Explorer starting Windows command interpreter for execution of a batch file, a copy of the environment table of currently running process is created by Windows for the new process. So whatever a process changes on its own local copy of environment variables has no effect on all other already running processes. The local changes on the environment variables are effective only on own process and all processes started by the process modifying its variables.
On starting the batch file the variables PATH and PATHEXT have the values as displayed on running in a command prompt window opened under same user account as used on starting the batch file the command set PATH listing all variables starting with PATH case-insensitive in name.
Now let us look on the second line of the batch file:
set path=C:\ rem default path
This line redefines the local PATH environment variable. Therefore the environment variable PATH being effective for the command process executing the batch file and all applications started by this batch file does not contain anymore C:\Windows\System32;C:\Windows;..., but contains now just this very strange single folder path.
C:\ rem default path
rem is an internal command of cmd.exe and must be written on a separate line. There is no line comment possible in batch code like // in C++ or JavaScript. For help on this command run in a command prompt window rem /?.
On running the batch file without an installation folder path as first argument, the result is that Windows command interpreter searches for dism.*, msiexec.* and xcopy.* just in current directory as there is surely no directory with name rem default path with lots of spaces/tabs at beginning in root of drive C:.
Conclusion: It is no good idea to use path as variable name for the installation folder path.
Another mistake in batch code is using %1% to specify the first argument of the batch file. This is wrong as the arguments of the batch file are referenced with %1, %2, ... Run in a command prompt window call /? for help on referencing arguments of a batch file and which possibilities exist like %~dp0 used below to get drive and path of argument 0 which is the batch file name, i.e. the path of the folder containing the currently running batch file.
I suggest using this batch code:
#echo off
setlocal EnableExtensions
set "SourcePath=%~dp0"
set "BatchName=%~n0"
if "%~1" == "" (
echo %BatchName% started without an installation folder path.
set "InstallPath=C:\"
goto StartInstalls
)
rem Get installation folder path from first argument
rem of batch file without surrounding double quotes.
set "InstallPath=%~1"
rem Replace all forward slashes by backslashes in case of installation
rem path was passed to the batch file with wrong directory separator.
set "InstallPath=%InstallPath:/=\%"
rem Append a backslash on installation path
rem if not already ending with a backslash.
if not "%InstallPath:~-1%" == "\" set "InstallPath=%InstallPath%\"
:StartInstalls
echo %BatchName%: Installation folder: %InstallPath%
echo/
echo %BatchName%: Installing .NET 3.5 ...
DISM.exe /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:installationMediaDrive:\sources\sxs
echo/
echo %BatchName%: Installing Python 2.7 64-bit ...
%SystemRoot%\System32\msiexec.exe /i "%SourcePath%py\python-2.7.amd64.msi" TARGETDIR="%InstallPath%Python27" /passive /norestart ADDLOCAL=ALL
echo/
echo %BatchName%: Installing Apache 2.4 64-bit ...
mkdir "%InstallPath%Apache24"
%SystemRoot%\System32\xcopy.exe "%SourcePath%\Apache24" "%InstallPath%Apache24\" /C /E /H /I /K /Q /R /Y >nul
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... for explanation of %~dp0, %~n0 and %~1.
dism /?
echo /?
endlocal /?
goto /?
if /?
msiexec /?
rem /?
set /?
setlocal /?
xcopy /?
And read also
the Microsoft TechNet article Using command redirection operators,
the Microsoft support article Testing for a Specific Error Level in Batch Files,
the answer on change directory command cd ..not working in batch file after npm install and the answers referenced there for understanding how setlocal and endlocal really work and
the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for understanding why using set "variable=value".
And last take a look on:
SS64.com - A-Z index of the Windows CMD command line
Microsoft's command-line reference
Windows Environment Variables (Wikipedia article)
The administrator of a Windows server should twist everything written here and on the referenced pages round one's little finger.
So basically, let's say I have the following folders in a directory:
test_folder_1
test_folder_2
And I also have a text file with the list of all the folders in that directory.
How can I create a script that will create a text file, test.txt, in all the folders in the directory? (test_folder_1 and test_folder_2)?
I tried modifying some code I found online and got this:
for /F "tokens=1 delims=," %%v IN (folderlist.txt) DO echo test>"C:\Users\myname\My Documents\test\"%%v""
However, running this, I get "Access Denied."
Any idea what went wrong, or alternative ways to do this? Thanks.
The command
echo test>"C:\Users\myname\My Documents\test\"%%v""
despite the wrong double quotes around %%v redirects the text test directly to the directory instead of a file test.txt in the directory. Or in other words: The attempt to create a file with name of an already existing directory is denied by Windows and the result is the access denied error message.
The solution for creating a file test.txt with the line test in each directory specified in text file folderlist.txt is:
for /F "tokens=1 delims=," %%v in (folderlist.txt) do echo test>"%USERPROFILE%\My Documents\test\%%v\test.txt"
It is also possible to create a file test.txt with a file size of 0 bytes in all subfolders of the test folder with following command line in the batch file.
for /D %%v in ("%USERPROFILE%\My Documents\test\*") do echo. >nul 2>"%%v\test.txt"
The command FOR returns in this case the subfolder name with full path always without surrounding double quotes even if the folder path contains a space character as it is the case here.
See How to create empty text file from a batch file? and read the Microsoft article about Using command redirection operators for more details.
echo. >nul 2>test.txt results in writing just a new line to STDOUT redirected to device NUL to avoid printing lots of blank lines on batch execution to screen. And to the file text.txt nothing is redirected from STDERR as there is no error message printed by command ECHO resulting in creating an empty test.txt file.
Run in a command prompt window for /? for help on command FOR.
I have an archive.pst file on my C: drive that I use in outlook to backup my email. But my C: is not backed up each night. So I'd like to copy the .pst file to my network drive so it will consistently be backed up. I do not want outlook to open the .pst file directly from the network drive for a variety of reasons.
Therefore I am trying to create a scheduled task that will copy my .pst file to a network location each day. The batch file below works perfectly if double-clicked. If I try to run the scheduled task, only the log file is created. Outlook doesn't close and the .pst file is not copied. I've tried running with the highest privileges but that doesn't seem to help. Any ideas would be appreciated.
cscript.exe close_outlook.vbs
::This is my VBS Script
::Set Outlook = CreateObject("Outlook.Application")
::Outlook.Quit
ping localhost > nul
set idrive="\\myserver\drive\\Outlook Files\"
set current="C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time% >> %idrive%\Log.txt
copy %current%\archive.pst %idrive%\archive.pst /y
echo End Time of Copy: %time% >> %idrive%\Log.txt
move %idrive%\Log.txt %idrive%\BackupLogs\Log.txt
ren %idrive%\BackupLogs\Log.txt %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
cscript.exe open_outlook.vbs
::This is my VBS Script
::set shell = createobject("wscript.shell")
::shell.run "outlook.exe"
EXIT
In reviewing the previous responses, I have shortened the batch file to only the code below. This works when double-clicking, but not when scheduling a task. I've also tried the same task moving the .vbs script to a network drive. Same outcome.
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\open_outlook.vbs"
1. Specify all files in a batch file executed as scheduled task with full path.
Double clicking on a batch file results usually in running the batch file with currently working directory being the directory of the batch file. But when running a batch file as scheduled task, the system32 directory of Windows is the current working directory.
Is close_outlook.vbs and open_outlook.vbs in system32 directory of Windows?
I don't think so. Replace in batch code below Path to\Script File twice by the right path.
2. Assign string values with space to environment variables right.
variable=value is the parameter for command set. With
set idrive="\\myserver\drive\\Outlook Files\"
you assign to variable idrive the value "\\myserver\drive\\Outlook Files\" with the double quotes included. This results on expansion of
echo End Time of Copy: %time% >> %idrive%\Log.txt
in the command line
echo End Time of Copy: 19:21:53 >> 1>"\\myserver\drive\\Outlook Files\"\Log.txt
and this is not right, isn't it.
Correct is:
set "idrive=\\myserver\drive\Outlook Files"
I removed also second backslash after drive and the backslash at end of folder path.
As the environment variable contains now the path with space(s) without double quotes, the double quotes must be added where the value of the environment variable is used concatenated with a file name, see batch code below.
There is one more reason why using "variable=value". A not visible trailing space at end of the line with command set in batch file is also appended to value of the environment variable if double quotes are not used or used wrong. Read this answer for details about correct assignment of string values to environment variables.
3. Define the wait loop using command ping better.
The command
ping localhost > nul
produces a wait. But it is better to use something like
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
as now the wait is determined with exactly 3 seconds.
4. Do not insert a space left of redirect operators > or >> for stdout.
Why this should not be done was explained by me here in detail.
5. Avoid environment variables not defined in batch file itself or in system account.
Your batch file uses only environment variables defined in batch file itself. So this advice is here not really needed.
However, many batch file working fine on double click but not on running as scheduled task fail because the batch file depends on environment variables like PATH or others which are related to current user account. It is safe to use environment variables which exist for all accounts like SystemRoot.
Reworked batch code
Here is your batch file with the appropriate changes whereby the paths of the two *.vbs files must be set correct by you before the batch file (hopefully) works as scheduled task.
%SystemRoot%\System32\cscript.exe "Path to\Script File\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
set "idrive=\\myserver\drive\Outlook Files"
set "current=C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time%>>"%idrive%\Log.txt"
copy /B /Y /Z "%current%\archive.pst" "%idrive%\archive.pst"
echo End Time of Copy: %time%>>"%idrive%\Log.txt"
move "%idrive%\Log.txt" "%idrive%\BackupLogs\Log.txt"
ren "%idrive%\BackupLogs\Log.txt" %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
%SystemRoot%\System32\cscript.exe "Path to\Script File\open_outlook.vbs"
set "idrive="
set "current="
I have a bzipping tool on my computer, but it only bzips files that are inside the "compress" directory. How would I make it so files inside all directories inside the compress directory are zipped?
Example
compress/image.png goes to compress/image.png.bz2
however
compress/folder/image.png stays as compress/folder/image.png
My batch file is as follows:
#echo off
title bzip
echo bzip
echo All files within /compress will be compressed as a .bz2
echo.
echo Compressing file(s)...
bzip2.exe -z compress/*.*
echo.
echo Compression Completed!
pause
I hope somebody can help me!
Edit:
When running the process with directories inside the compress directory, it says "permission denied".
Use for /r compress %%i in (*) do bzip2.exe "%%i" in your batch file instead of the call to bzip2.exe directly. bzip2 almost certainly doesn't know how to recurse through subfolders -- standard wildcard globbing libs on Windows generally don't.
Run for /? from a Command Prompt to see more about the syntax of the for command. If you want to test the command from a prompt instead of a batch file, use 1 percent sign for the variable instead of 2.
In C you can use %username% as a variable for the current user's name for directory listings and such: c:\documents and settings\%username%\
Is there something like this for a batch script?
Using just %username% doesn't seem to help.
I wrote a script that accesses my FTP server so I can load files to the server.
I want my friends to be able to use this script, but I don't want to have to write several different scripts.
Here is what I have so far:
#echo off
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
lcd "c:\documents and settings\%username%\my documents\FTP"
mput *txt
pause
bye
There's gotta be a way
This can be done if you change the batch file so that it creates a script file every time the batch file runs. You can do this by using the echo command to write the script lines to script file, which you can then pass to the ftp command. The reason this works is that echo will expand the %username% variable before writing it to the script file:
#echo off
del script.txt
echo open FTP.server.com>>script.txt
.
[echo rest of script lines to file]
.
echo lcd "c:\documents and settings\%username%\my documents\FTP">>script.txt
echo echo mput *txt>>script.txt
#ftp -s:script.txt
I believe i found a better way, although it's a bit more code.
set "rootdir=%userprofile%\my documents"
set "destdir=c:\
for /f "delims=" %%a in ('dir /b /s "%rootdir%*.txt"') do copy "%%~a" "%destdir%"
And then the usual FTP stuff, including lcd c:\
Ive tested this and it works, although I would like to find a simpler way.
I tried using xcopy but for some reason it doesn't work on my system, the cmd screen just hangs.
Also tried just using copy, but that gave me "can't find file" errors.
Instead of using lcd, a better idea might be to change the working directory in the outer batch file.
#echo off
#pushd "c:\documents and settings\%username%\my documents\FTP"
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
mput *txt
#pause
The only problem with this solution, is that the script itself is no longer in the working directory, and so you need to add a path for that. (Or, put it in the FTP folder ;)
Also, minor pedantry, but this is not actually a correct way to find My documents. In particular, on Vista or Windows 7, User profiles are stored in C:\Users. And, it's possible for users to move My Documents (on my computer, My Documents is located in D:\Mike's Documents)
However, there doesn't appear to be an environment variable that points directly at My Documents, so you will have to make do with this:
"%userprofile%\my documents\FTP"
If the people running this script are running XP and haven't moved their My Documents, then this doesn't really matter.