-1

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"

JR Santos
  • 137
  • 1
  • 11
  • 1
    Type `set` to see what it means. – user14797724 Jan 14 '21 at 06:51
  • 1
    Open a [command prompt](https://www.howtogeek.com/235101/), run `set` and displayed are all [Windows Environment Variables](https://en.wikipedia.org/wiki/Environment_variable#Windows) defined for your user account. Run `set user` to get displayed only the environment variables of which name starts with `user`. `"%userprofile%\main\folder\files"` with the double quotes is correct for referencing the folder. Please run also `call /?` and read the output help. It explains how to reference arguments of the batch file. Argument 0 is the batch file itself. `%~dp0` references batch file path. – Mofi Jan 14 '21 at 07:24
  • If you want to `call` another batch file like `edit.bat` from within a main batch file stored in the same directory as main batch file, `call "%~dp0edit.bat"` is the correct command line. Please note that `%~dp0` always expands to batch file directory path ending with a backslash. For that reason the concatenation with a file/folder name or wildcard pattern should be done without an additional backslash as this results in two backslashes in series within entire argument string which Windows has to correct on accessing the file system by replacing ``\\`` by just ``\``. – Mofi Jan 16 '21 at 18:39

2 Answers2

2

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Leavii
  • 82
  • 2
  • 12
0

If you want to open a file in your portable drive you can do something like this

%~d0\folder\folder\file

JKC
  • 79
  • 6
  • Thanks everyone I will put this together, But I think I have it now, I will follow up if It worked – JR Santos Jan 14 '21 at 14:36
  • OK so that will load from a portable drive, can this also work for local C: Desktop, or for that I need to use %userprofile%\folder\folder\file – JR Santos Jan 14 '21 at 15:37
  • OK so to run it up from usb would it be like this `call %~d0\Main\folder\edit.bat` – JR Santos Jan 14 '21 at 23:53