Variables in Batch FTP script - batch-file

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.

Related

set /p failing to read sftp mapped file

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)

Debugging a batch file

I wrote a batch file that is supposed to iterate through all .csv files in a folder and do the following with each one: MOVE/Rename it to solar.csv in a different folder while overwriting any file with the same name, start some service that utilizes solar.csv, wait 7.5 minutes, stop the service. This should be done for each .csv file in the folder. I would like it to report each step as it does it as well. Here is my code so far:
#ECHO OFF
CD "D:\PI Data Import\Data"
For /R %%G IN (*.csv) DO (
MOVE /Y %%G.csv "D:\PI Data Import\solar.csv"
ECHO "%%G.csv Moved"
NET START RDBMSPI4
ECHO "Service Started"
ECHO "Waiting"
TIMEOUT /T 450 /NOBREAK
NET STOP RDBMSPI4
Echo "%%G.csv Complete"
)
It reports that my command syntax is not correct, but doesn't say which part exactly. I assume the FOR /R line is a bit wrong, maybe the MOVE command, and possibly how I handle the %%G in the command section.
All I really need is a syntax check, and I am afraid to test this too terribly much before it is golden since the server it will operate on is kind of important. All help appreciated, I'm not at all experienced with working on Batch files.
I'd remove the /R from the for command. Also, don't put .csv after %%g anywhere. They will already have .csv appended as they are .csv files.
You're going to overwrite the solar.csv file, every time you move a CSV file BTW. If you wish the CSV files to be appended to solar.csv, then you may have to type them out to that file. E.g.
Before you loop...
if exist solar.csv del solar.csv
And in your loop...
type %%g >> solar.csv
Unless your service is going to process each solar.csv file and then you just overwrite the file with the next one, in which case you're golden :)

CMD mkdir syntax error

I am kind of new to cmd for I am a linux guy. In the code bellow I am trying to find a .mp3 or .mp4 file and save there path to a file and then move the .mp3/.mp4 into the file. I get a syntax error on the mkdir command
ECHO off
pause
mkdir "/Users/media"
pause
cd /Users/
dir *.mp3 >Users/media/output.txt/s
dir *.mp4 >>Users/media/output.txt/s
pause
for /r %%a IN (*.mp3) do (
move /y "%%a" "/Users/media"
)
pause
for /r %%a IN (*.mp4) do (
move /y "%%a" "/Users/media"
)
thanks any help would be appreciated
Windows supports forward slashes in many scenarios but prefers backslashes. So you should change the appropriate line to
mkdir \Users\media
If your path contains spaces you have to surround it with quotation marks. In case the users directory does not exist you can add a -p to the command which will have it create the complete hierarchy you specify.
Depending on how you use the batch you might want to add a drive letter to your path and check the errorlevel of the mkdir command.
Read more about mkdir here, this site lists the other available commands too.
As you are coming from Linux I want to mention that bash and others can be installed on Windows too, there even are UNIX "emulations" like Cygwin. There are alternatives to batches, for example Windows scripting host which looks more like regular programming and adds support for vbscript and JavaScript. Or you have a look at powershell.
Both alternatives create (but I am maybe biased) better, more readable and maintainable code. Batches are often a pain to those that follow you and have to understand and change.

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

bzip2 - Bzipping all files inside folders (Windows)

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.

Resources