If I have call C:\Users\user\Desktop\Main\folder\edit.bat
What is the right way to set this up this:
I want to eliminate C:\Users\user\Desktop, and make my batch universal so that I can plug in to any computer, run it from a usb, or external or simply add my batch to the desktop of anyone's computer. And run it without having to edit source or target again.
If I want it to run my edit.bat from any computer do I set it up like this
call %SystemRoot%\Main\folder\edit.bat
or
call %UserProfile%\Desktop\Main\folder\edit.bat
or
call %UserProfile%\Main\folder\edit.bat
If I understand Mofi right I can change this
call "C:\Users\user\Desktop\Main\folder\edit.bat" Original
to this
call "C:\users\%username%\Desktop\Main\folder\edit.bat" Works
and to this
call "%UserProfile%\Desktop\Main\folder\edit.bat" Works
Can anyone tell me if this is the correct
call "%~dp0\edit.bat"
If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\ or %userprofile%\documents\.
#echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist checks for the directory. mkdir creates the destination directory. Pretty obvious but none-the-less.
%~dp0 defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile% environment variable is by default equal to the directory C:\users\%username%\, and %computername% expands to the computer's name.
xcopy takes the source directory and copies to our destination we created prior. /Y forces the copy and does not prompt to overwrite files.
* is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\* will not resolve. For something like that you would need to use for /D.
Lastly I always like to check for errors, and see if it was successful. If not we pause to make sure we see the error, or we put in a timeout /t seconds to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.
If you want to open a file in your portable drive you can do something like this
%~d0\folder\folder\file
Related
We run NetSuite for our shipping software and use Zebra ZP450 printer to print the thermal label.
The software downloads a .zpl file which I have assigned to run with a batch file, script here:
Net use LPT2: \\%ComputerName%\ZebraFedex
Copy %1 LPT2
Net use LPT2: /Delete
I have installed this on probably 20 or more computers without an issue. But the last two will initiate the batch file but not print.
I was able to hit pause on the batch file and here is what I got:
C:\users\noah\downloads\net use LPT2: \(ComputerName)\ZebraFedex
The command completed successfully
C:\Users\Noah\Downloads\Copy LPT2
The system cannot find the file specified. 0 files copied
C:\Users\Noah\Downloads>Net use LPT2: /Delete
I've checked my file association, which seems correct since I see the command prompt flash on the screen. I have no idea here...
Working successfully on another computer
Problem on puzzling computer
Here's my batch to transfer file to my venerable TLP2844:
#ECHO OFF
SETLOCAL
FOR %%a IN (%*) DO (
IF EXIST "%%a" (COPY /b "%%~a" \\%computername%\TLP2844
) ELSE (
IF EXIST "%%a.txt" (
COPY /b "%%~a.txt" \\%computername%\TLP2844
) else (
ECHO Neither "%%a" nor "%%a.txt" appears to EXIST
)
)
)
where I have a printer called TLP2844 installed as a ZDesigner TLP 2844 device on USB003.
The problem, as shown by the graphics I've included in your question (saves running off chasing links) is that the label file C:\users\melan\downloads\UPSSHIPPINGLABEL1700448.zpl is being provided to the batch as %1 (first parameter). On the problem installation, no filename (presumably C:\users\Noah\downloads\UPSSHIPPINGLABEL???????.zpl is being provided to the batch as %1.
Therefore it's not a batch problem, but a problem with Netsuite.
From your .pdf, on page 26 :
To install a thermal printer driver on a Windows PC:
...
5. In the Share Name field, enter the name of the printer. For example, LP 2844.
The printer name cannot contain spaces.
So you're told the printer name cannot contain spaces, but the example printer name contains a space....Excellent! (shows my level of confidence in the product, but not actually directly relevant to the problem) :(
Personally, I'd change your batch file to
Copy %1 \\%ComputerName%\ZebraFedex
Perhaps you'd try that after fixing the problem where Netsuite fails to deliver the filename to the batch. Perhaps it's set up to produce .pdfs on the computer in question (That's a wild guess)
In response to reply:
Please change your batch file (temporarily for test purposes) to this:
Echo File to print : "%1"
Net use LPT2: \\%ComputerName%\ZebraFedex
Copy %1 LPT2
Net use LPT2: /Delete
PAUSE
which will report the filename supplied by Netsuite to the batch file on the console window, then keep the window open at the end of the batch until the open window receives a keystroke.
Please report results for both a "working" and a "buggy" installation.
Change .bat file like below, replacing the printer share name with your own. Happened on one of our critical computers that is also the host for FedEx/UPS.
C:\WINDOWS\system32\net.exe use LPT2: \\%ComputerName%\Zebra_ZP500_Shipping_FedEx1
Copy %1 LPT2
C:\WINDOWS\system32\net.exe use LPT2: /Delete
Thank you Noah Schultz for the Pause suggestion for batch file, and to QWERTY for article on Net command issue
Reference
I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.
I am trying to copy a file and folder from on my flash drive to the desktop of any computer I plug it into. I am somewhat familiar with the command line and batch scripts but have not used it for doing file manipulation in this way.
I originally wrote a script to test copy it from my flash drive to my desktop, however, the issue is on a new computer I won't always have the same drive letter assigned.
The code I used to test copy everything with known drive letters where the script starts in the folder on my flash drive where what I want to copy is:
copy aFile.file C:\Users\*user*\Desktop
where user is my user folder.
The main issue I am running into right now is copying things from one drive to another where it needs to go into a user profile
cd has the /D switch which specifically specifies to switch drives.
so
cd /D %userprofile%\Desktop
goes to the active user's Desktop... right where I want it.
whereas
cd %userprofile%\Desktop
does nothing because users are on a separate drive.
Is there something similar to /D for copy? The main issue is that drives letters change and user names change.
User name changes are easy to deal with because I can use %userprofile\Desktop.
If we assumed that copy made use of the same switches as cd (it doesn't) then what I basically want is:
copy /D aFile.file %userprofile%\Desktop
I am sure I am missing something relatively simple but I didn't see any switches that seemed to be what I need.
I am also open to using robocopy if that would be easier, but attributes (timestamp, owner, etc) don't matter to me so I didn't think it would benefit me immediately.
batch scripts may not be compatible with cd
try to make a script that asks two inputs.
#echo off
set /p var1="this is the file that will be moved"
set /p var2="this is the destination"
xcopy %var1% %var2%
echo copied!
pause >nul
this will ask for two inputs,first input asks for the file,second one is the file copy to
I have a program with a separate setup for 32 and 64 bits. My goal is to create a single executable that can run the appropriate setup. So I created a folder and placed the two setups inside, then wrote the following script:
#echo off
if %PROCESSOR_ARCHITECTURE%==AMD64 goto :x64
if %PROCESSOR_ARCHITEW6432%==AMD64 goto :x64
:x86
"%cd%"\setup.exe
exit
:x64
"%cd%"\setup-x64.exe
exit
Afterwards, I created the SFX file with this folder in WinRAR, pointing to the BAT file. But when I run it, a command line window pops up and shuts down instantly and nothing happens. I go to the temporary folder and double click the BAT file and the setup starts. The same happens in the original folder. What is happening and how can I fix it? Thanks!
%cd% reffers to the directory of the call of the batch-file.
For example a batch-file is in %USERPROFILE%\Desktop\Folder\bat.bat:
echo %cd%
pause
and you start it for example from the command-line like this:
C:\>%USERPROFILE%\Desktop\Folder\bat.bat
it should echo C:\ as that is where you called it from.
Two ways from the comments to solve the problem:
Push the directory of the batch-file using pushd "%~dp0" -> will result in a change of the variable value of %cd%
or
do not use "%cd%" but "%~dp0"
Both ways use the fact that the zeroth argument of a batch-file is its path.
You can prevent the command-line window from closing if you are debugging the file from the command-prompt itself if possible. With that you should have seen an error that state something like ...\setup.exe not found. After that nothing had to be done from the batch-file so it closed.
M'kay, so I've written a few batch files before, so I'm not completely new to them, but this is stumping me. What I'm trying to do is run a .exe file from a batch file. Here's the Batch script:
#echo off
:start
setlocal EnableDelayedExpansion
cd "C:\Users\Zac\Dropbox\SoundCloud"
set n=0
for %%f in (*.html*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
move "!file[%rand%]!" C:\Users\Zac\Temp
start "~dp0Link_Open.exe"
echo %time%
timeout 70 > NUL
echo %time%
goto start
So from my understand, this moves a random .html file from one directory to another, this works, I've used it a lot, the only issue is the "Start" command, I don't use this very often. the "Link_Open.exe" is in the same folder as my .bat, but I've tried running it with the full directory written in, I've tried quotations, no quotations, brackets, no brackets, START, start, Start, Call, CALL, call, and none of them work, I'm always getting the same error "Link_Open.exe cannot be found, have you written it correctly"
The only reason I can possibly think of that would be why it wouldn't work, is that the .exe was written in AutoIT and then compiled...but that shouldn't effect it should it?
Running the batch file will result in a random file being moved, and then an error coming up, and then repeating.
What am I doing wrong?
Ps: Running the Link_Open.exe does what it's supposed to do, so there's no errors there, the only issue I'm having is opening it with .bat.
I'm still very new to Autoit, but if someone could show me a script to move a random .html file with Autoit, I could just combine the two scripts together couldn't I?
Provided that your bat file and exe file are in the same directory as you said, try changing:
START Link_Open.exe
to:
start "%~dp0Link_Open.exe"
%~dp0 expands to the current batch script's path
start equivalent to START, it doesn't matter
enclosed in quotes in case your path has spaces like C:\My Documents, thus keeps the whole path as one argument, instead of being misinterpreted as being multiple arguments separated by space
Error Explanation
Your error: Link_Open.exe cannot be found... is precisely that, batch can't find Link_Open.exe. Normally it would, because when you just give a program without full absolute path, it checks the current working directory. This defaults to where the batch script is. But notice you actually changed the current working directory with this line:
cd "C:\Users\Zac\Dropbox\SoundCloud"
Thus, unless you happen to have Link_Open.exe in that SoundCloud directory, or unless you had any code to cd back to the batch script's directory containing the exe, or unless you happen to have Link_Open.exe in %PATH%, then there is no reason batch would automatically know to check the original directory that the script was run from.
Thus we try to refer to Link_Open.exe in a way that batch will know how to find it. One way is if we did absolute path C:\path\to\Link_Open.exe however to keep things portable, I recommend %~dp0 which expands to whatever path of the current batch script, and thus batch is able to find Link_open.exe again.
You change directory to C:\Users\Zac\Dropbox\SoundCloud and then try to start Link_Open.exe which is left in script directory.
So next step you use ~dp0 variable to get folder from script executed, but there is missed leading %: you must write %~dp0
Even though you'll fix it, maybe Link_Open.exe also depend on current working directory? start allows to set up CWD: start "window header" /D "%~dp0" "%~dp0Link_Open.exe"
To debug: add echo before start and add pause next line. start command will be printed with all variables expanded. You'll see if the something is wrong - e. g. you may copy and test via Start -> Run.