Failing at Using PUSHD/POPD on Multiple Directories - batch-file

I am automating a file download thorough CuteFTP using VBScript. At the end of the VBScript I am calling a batch file that will use 7zip's CLI to expand the zip file.
The batch file and the 7zip executable are stored on Server1 (nt950id3). The expansion takes place on Server2 (nt950a1). Due to corporate restrictions, this cannot be changed. Since the VBS is working, I have omitted its code. This is the batch command-
"\\nt950id3\c$\apps\CFI\7zip\7za.exe" e -y "\\nt950a1\filexfr$\Spectrum\File.zip" -o"\\nt950a1\filexfr$\Spectrum"
Expansion is accomplished awkwardly because 7zip will extract to the CWD of the batch file, as opposed to the directory the zip file is in unless I specify the -o switch.
When I double-click the batch file or run it via a scheduled task, it works just peachy. When VBScript calls the batch file, it fails stating UNC paths are not supported - I did not know there would be a difference. PUSHD and POPD should fix this.
Further research through this post on CLI Crash Course lead me to use PUSHD on each directory and set them as variables-
SET UZEXE=PUSHD "\\nt950id3\c$\apps\CFI\7zip\7za.exe"
SET ZSRC=PUSHD "\\nt950a1\filexfr$\Spectrum\File.zip"
SET ZEDST=PUSHD "\\nt950a1\filexfr$\Spectrum"
At the bottom of the code I use 'POPD' three times (also attempted at the end of each line) and despite having this very simple SO post on setting paths I am unable to make this work. I also attempted this without PUSHD-
SET UZEXE="\\nt950id3\c$\apps\CFI\7zip\7za.exe"
This also failed stating UNC paths are not supported.
Could the community kindly explain my errors and point me towards an example of how I can accomplish running a command using multiple, separate PUSHD/POPD directories?

Following Bill's guidance, I used only VBS - no calls to batch - for my issue. I was not aware this was so easily possible and will endeavor to do better research going forward. The working code - with stand-in UNC paths - is as follows:
Set wshShell = CreateObject("wscript.shell")
wshShell.Run"""\\server1\path\7z.exe"" e -y ""\\server1\path\srcfile.zip"" -o\\server2\path\unzipdest", 0, True
At first, the executing code stated it could not find the file. This turned out to be an issue with white space in the file paths. After using this Google groups topic to learn to properly place the quotes, it is now working within the CuteFTP script with no errors.

Related

Batch File - Excluding certain file types when executing a command to all files in the same folder

Been working on creating a flexible batch script that when you drop it into a folder and run it it will will run it's command on every file in that folder and subfolders but exclude specific file types such as itself. This has proved far more difficult than originally thought and would be thankful for some insight.
Goals of the batch file:
Running command on files that lack a file extension
Does run command on files with a file extension
Relative paths for flexible use when dropped into folders
Helpful to run through child folders as well, but not required
What me and a colleague have come up with thus far for checking functionality is this:
set FILEFORMATS=".bat"".ini"
for %%x in (*) do if /I not %%x == (%FILEFORMATS%) (echo If Not Command) else (echo If Command)
The problem we think we're running into is the if statement seems to check all files in the folder as a whole and executes based on that instead of checking if the statement is true for each file individually in the folder.
Thanks in advance

How to specify in windows batch file to open spyder, change the working directory and run a script?

I have created a windows batch file to run a code which is located in a particular folder. However, I am running the batch file but it does not work. What I want it to do is:
Change working directory in Spyder (Spyder is already openned so I don't want it to open it first)
Run the script I want to execute
Basically, the script I am running is plotting a bunch of graphs and all the files needed to create this are located in the working directory.
#echo off
"C:\Users\Mason\Desktop\WinPython-64bit-2.7.6.4\spyder.exe"
chdir /D "F:\optimisation"
"F:\optimisation\plot.py"
pause
The batch file runs fine without any errors, but nothing happens in Spyder e.g. it doesn't change the working directory and neither executes the code.
I'd suggest
#echo off
setlocal
chdir /D "F:\optimisation"
"C:\Users\Mason\Desktop\WinPython-64bit-2.7.6.4\spyder.exe" "F:\optimisation\plot.py"
pause
That is, switch to the required directory, having set a local environment (this restores the original environment when the batch terminates).
Then run the spyder executable, providing it with the name of the file. It would be normal practice to use this structure to provide a significant filename to an executable (eg notepad fred,txt)
Since the current directory when spyder runs is f:\optimisation, it is probably not necessary to specify the entire path in spyder's argument.
Note this is all just speculation using normal practice. I have no experience of spyder- in fact, this is the first I've heard of it.

Using 'for' command to find and download the latest file on SFTP server in PSFTP

I have gone through the following guide to set up an SSIS package to retrieve a text file located on an SFTP server:
https://www.mssqltips.com/sqlservertip/3435/using-sftp-with-sql-server-integration-services/
To summarize, the SSIS package executes PSFTP.exe (A PuTTY tool) which takes the necessary credentials to connect to the server. It also takes a batch file that it executes after connecting. This batch file contains the commands to retrieve the desired text file. To start from the guide, it simply contains a cmd command to change directory, and a get command to retrieve the file:
cmd DataDump
get TeleMarketingResults.txt
All of this works fine.
The issue arises when I try to make this batch file logic more complex as it does not seem to recognize basic keywords. For instance, I would like to modify it to retrieve the most recent file, so I tried adding this:
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo The most recently created file is %LAST%
but then I get these errors:
psftp: unknown command "for"
psftp: unknown command "echo"
If I execute the batch file manually in a local directory, it works. The issue only occurs when passing it as a parameter to PSFTP.exe. Why is this?
psftp script file can contain psftp commands only. for, set or dir are not psftp commands.
There's hardly any reasonable way to retrieve latest file using psftp. You would have to do it in two steps. First to retrieve listing and store it to a file. Then parse that file using some smart batch file commands to find the latest files. And then run psftp again to download that file. It is cumbersome and ineffective as it requires two connections.
You better use a more powerful SFTP client. For example it's trivial with my WinSCP SFTP client. See
Question WinSCP select most recent file or
WinSCP article Downloading the most recent file.

Command Line Installation with SCCM 2012

I have a few applications that I am trying to deploy with SCCM 2012 but the installations are failing through the application catalog. So what I have for the deployment type is a script installer. I have "cmd.exe" (Without quotations) in the Installation program field and "Installer.bat" in the installation start in field.
When I look at the ccmcache folder, all the contents over that application are there but the following error displays the Software Center:
0x8007010B(-217024629)
I have done some reading online and the "10B" is a common command line error for invalid directory. I have tested the batch file when hard coding a path but my question is, how can I edit the batch file or SCCM to pull from the CCMCache path where the files are downloaded to on the local client? Currently the Batch File is simply:
#echo off
ApplicationName.exe
Do I need to edit the file to cd into the CCMCache folder where the files are placed? How can I get the batch file to run the executable that is downloaded to the CCMCache folder?
Thank You!
You need to have the full path to the installation in your script
#echo Off
\\path to .exe
The way the command is written will not be able to find the .exe file. You need to add the full unc path to the .exe into your .cmd file. You should have your installation .exe and .cmd file in the same location on the distribution share
Recommended Solution:
Before starting, since you are only launching an exe with your batch file, I would recommend just using your ApplicationName.exe as your command line parameter in SCCM instead of using a batch. This will eliminate the need to engineer any further.
Modifying the existing solution to work:
If you do still want to use a batch file, keep a few things in mind. The syntax you are using to launch the batch file will not work. I would recommend just using the batch file name "installer.bat" as your command line. If you still want to preface the batch with the cmd.exe, you absolutely need to use the /c switch with it
cmd.exe /c installer.bat
If you don't use /c the console host will only open to a promopt and not execute your batch.
This is not an ideal solution though because using "cmd.exe /c" will set your working directory to the location of cmd.exe (ie "C:\windows\system32") and since your content is staged in ccmcache, you will need to specify its location within your batch. To do this, you would use %~dp0 variable which gives you the directory the current batch is running from. This means modifying your batch to read
#echo off
%~dp0ApplicationName.exe

Jenkins coping with spaces in batch file arguments

I'm setting up Jenkins on Windows to take care of the builds for a Windows project I've been working on. Until now, I've just been building locally. I've got a batch file that performs the build, which ends up generating some msi installers.
Some of the projects contain post builds steps that run batch files. The arguments to the batch files sometimes contain spaces. This isn't a problem when I run my build batch file from the command prompt.
However, Jenkins seems to have a problem with this - I get errors such as
"File not found: C:\Program"
I'm puzzled as to why the error happens when Jenkins triggers the batch, but not when I run it manually - any ideas?
For arguments that include spaces, wrap them in double quotes. Example:
WRONG: PROCESS_FILE.EXE C:\Program Files\This File.txt
CORRECT: PROCESS_FILE.EXE "C:\Program FIles\This File.txt"
As Jason mention, you need to pass your arguments and paths with quotes.
Also, check the build log and see which type of quotes you're using. Depending on where it's being passed you may need single or double quotes, or some sort of escape character.
If you want to look at the actual batch file that Jenkins runs for your external commands or pre/post build events, check the build log and load up the temporary batch file in an editor to see what it actually looks like. Sometimes this is necessary to debug your build.

Resources