I have a batch file that needs to copy my EXE to desktop and run it from there.
Code:
copy client.exe %USERPROFILE%\Desktop
%USERPROFILE%\Desktop\client.exe
What seems to happen is that client.exe is indeed copied to desktop and ran but acts as if it is in the directory of the original client.exe
The problem is that as far as the batch is concerned, the current directory is wherever the batch is execute from.
If you want the current directory to be the desktop, you'd need to explicitly set it
copy client.exe %USERPROFILE%\Desktop
pushd "%USERPROFILE%\Desktop"
client.exe
popd
or
copy client.exe %USERPROFILE%\Desktop
cd "%USERPROFILE%\Desktop"
client.exe
The first temporarily switches the current directory, so it is restored to what it was when run after client.exe terminates; the second makes a permanent switch to the desktop.
If you want start an application and specify where files will be saved (Working Directory), use either
CD /D "%USERPROFILE%\Desktop"
"full_path_to_app\client.exe"
or
START "" /D"%USERPROFILE%\Desktop" "full_path_to_app\client.exe"
Copying the .exe file seems to be a needless action.
Related
I have a really messy program in my environment that basically has a jump client on over 7000 machines in my environment. When I upgrade the appliance that the jump client talks to it starts an in place upgrade but if there are computers off or off the network obviously the upgrade times out. Problem is, I cant mass deploy the updated jump client because the previous version has to be uninstalled first.
WMIC uninstaller doesn't work, msiexec uninstaller doesn't work. There is a batch file built into the program stored at %ProgramData%\ClientNameRandomNumbers. Problem is, you can also install a dissolvable client in the moment if needed and everytime the dissolvable client installs, it doesn't clean up after itself. So you have random folders that may or may not be the one that contains the uninstall.bat batch file that I need to run. I wanted to write a script to mass deploy that will start that batch file on each computer but I am having problems.
Basically I want it to search for the folder with a wild card, if it finds it CD to that directory and then try to run the uninstall, if it can't find the uninstall, continue through to deleting the folder as it is an old shell folder. And then go back and look for more folders until it can't find anymore and then go to exit.
This is what I have:
:START
cd %programdata%\bomgar-scc* || IF ERRORLEVEL not = 0 GOTO END
start uninstall.bat /Wait || cd c:\programdata &&
rd C:\programdata\bomgar-scc*
GOTO START
pause
:END
EXIT
Any suggestions?
I think I am understanding your requirements but maybe not. I have commented the code to let you know what it is doing.
#echo off
REM For command will attempt to find all folders
REM that start with bomgar-scc in the programdata folder
for /d %%G IN ("%programdata%\bomgar-scc*") do (
REM If it finds a folder check if an uninstall.bat
REM exists and run the batch file if it does exist.
IF EXIST "%%~G\uninstall.bat" CALL "%%~G\uninstall.bat"
REM Remove the directory found
rd "%%~G"
)
Create a Jump client MSI installer. Run the following command to uninstall it.
msiexec.exe /x "\%networkpath%\bomgar-scc-win64.msi" /quiet
I'm trying to automatically move the subfolders and files from my Dropbox folder on my F: Drive to a separate folder on the same drive, therefore emptying my Dropbox and freeing up space in it while backing up the files.
I tried this in Batch:
MOVE /-Y "F:\Dropbox\files\camera" "F:\backup\Camera\"
pause
but I keep getting Access Denied even when running as Administrator.
I also tried this in VBS:
With CreateObject("Scripting.FileSystemObject")
.MoveFile "F:\Dropbox\files\camera*", "F:\backup\Camera\"
End With
but I only got Path Not Found from that.
So pretty much I'm a bit stumped, or overlooking something obvious, But basically I just want to make a small script in vbs or batch that allows me to move all the sub-folders and files from F:\Dropbox\files\camera\ to F:\backup\camera\ so I can set it as a scheduled task and let it run each day so that it empties my Dropbox folder(and therefore my Dropbox account) of all files and folders and backs them up.
Any help would be appreciated, I have already searched a number of different options and none seems to work specifically for my purpose.
I suggest using ROBOCOPY instead of MOVE.
I have a similar backup script that uses it.
See:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
#ECHO OFF
ROBOCOPY /E /MOVE "F:\Dropbox\files\camera" "F:\backup\Camera\"
MKDIR "F:\Dropbox\files\camera"
Options:
/E : Copy Subfolders, including Empty Subfolders.
/MOVE : Move files and dirs (delete from source after copying).
Because of the /MOVE switch, I need to re-create the source directory because ROBOCOPY moves it to the destination directory. ROBOCOPY, by default, will retry the operation if it fails. See the /R:n and /W:n options to customize it. Also, the command will print a lot of info messages to the terminal, but you can customize it using the ROBOCOPY's logging options (ex. /NJH, /NJS, etc).
For the "Access Denied" error, make sure that:
You have write access to the destination folder
(Test by creating a batch file with just MKDIR "F:\backup\Camera\some_file.txt")
(Test by creating a batch file with just MKDIR "F:\backup\Camera\some_folder")
The files being moved are not being used or opened anywhere prior to running the script
(Ex. It's not opened in the Dropbox app.)
xcopy "D:\CCStudio\rtos" panasonic /s /e
The folder panasonic and the bat file are on the same level in the directory structure, although on another machine I have access to.
When I use the bat script on my PC everything works fine. But when I put the bat script into the remote shared folder, it doesn't work as expected. The problem seems not to be the source, instead the destination address is the key in the problem. When I replace `pansonic' whit its absolute address, the script works.
So why should I specify absolute path for destination? Recall that the destination is external path. It is shared folder on another windows machine.
I currently run a bat from a shared drive to copy to another shared drive. This is what I use to get it working fine. Adjust as needed.
cd /d %~dp0
xcopy /s "D:\CCStudio\rtos" "panasonic\" /E
cd /d %~dp0 will change the directory to what ever the batch file is in allowing you to use relative paths. This script will copy the files in rtos to panasonic. Folder path will look something like this X:\Shared\Network\panasonic\RtosFiles.ini assuming RtosFiles.ini was in D:\CCStudio\rtos and the batch was run in X:\Shared\Network\
I know from a desktop I can have a bat file that runs an executable with specific parameters and starts in the same path on the network where the executable exists, but how can I accomplish the same thing when calling the bat file assuming is in same folder as executable from another application?
For example:
\My-Network\app\PR8.exe /noload
I want to start specifically in \My-Network\app (same folder where PR8.exe exists) but not where it defaults to which seems to be c:\windows somehow. I can't seem to do a cd on a UNC path and I don't want to use any path letters as the application also detects as to which server it is executing from as well.
It isn't possible to set a UNC working directory for Windows batch files without network mapping drives. However, in Windows 2000 and later, you can use the PUSHD and POPD commands to change the working directory to a UNC share when running your script.
Wikipedia gives us the example of creating a shortcut to your batch file where the Target is set to the following:
%COMSPEC% /E:ON /C "PUSHD """\\My-Network\app\""" & C:\PATH\TO\BATCHFILE.BAT & POPD"
In this case the Working directory attribute of the shortcut is ignored, as the PUSHD and POPD commands will change it to the path specified.
done a search on this, but little joy. I am making and later, trying to delete a folder in the same batch file:
mkdir "%_gameKey%"
:: Stuff.....
rmdir /s /q "%_gameKey%"
I get the error "the process cannot access the file because it is being used by another process." I also can't delete the folder via right-click delete - same error. Interestingly, the rmdir removes the contents of the folder. There is nothing in the folder, nor is there anything obvious accessing it.
I have used Unlocker - shows no processes. Have also used Process Explorer, searched on the folder name (with/without full path) - no search results.
When I restart my machine, I can delete it. Not sure if down to user permissions. I see from this that Users only have Read/write not full control. (sorry - not very good with user permissions stuff - especially on windows!). However - surely it can't be this as I can delete it upon restart.
The folder is on a non OS drive.
I'm stumped - any ideas?
A folder cannot be deleted on Windows if any application uses this folder currently as working directory.
In your case I suppose that the batch file uses command cd to change the working directory to the created directory. So you need to use cd once again to set a different working directory before using command rmdir to delete this directory.
It is of course no good idea to start other applications with working directory being currently the directory which should be deleted later and which continue running on exit of batch file as those started applications have this directory as working directory as well.
I had same issue, after launched a powershell process from a folder via win cmd.
After powershell finished, I cd .. in win CMD but rd /q /s gave error: open in another process
I get same error when manually delete folder in explorer window, even if it was empty.
wmic process revealed that powershell.exe could be locking the folder, even though powershell had finished the task & powershell window had closed.
This was confirmed by tasklist
I then taskkill /im powershell.exe, after which the folder could be deleted.
My word - that was an obscure one, but finally found the answer by stepping through the code putting the rmdir in various places. I post this here in case anyone encounters the same...
Basically, I was calling cmd prompts and launching external exe's whilst in the said folder. Later, I was forcibly closing the exe's with TaskKill, but this was leaving the cmd consoles open at the path of the directory I was trying to delete. I'll try be clearer....
Whilst batch script was in the directory (e.g. C:\Windows\Scripts\Launch), I used the following command:
Start "AppLaunch" Call "!_supExe!" !_supCmd!
Which, after expansion, translated to, for e.g.:
Start "AppLaunch" Call "C:\Windows\Program Files(x86)\Troubleshooter\trbshtr.exe" -game "My Game"
I was using this because, from reading, there appears to be a bug in windows making the following command fail:
Start "AppLaunch" "C:\Windows\Program Files(x86)\Troubleshooter\trbshtr.exe" -game "My Game"
The problem comes when you have to use quotes for both the exe and the parameters (for instance where there's spaces). Windows doesn't read it right. Thus had to use topmost to launch these apps.
Problem being, when used:
taskkill /f /im trbshtr.exe
The exe was killed, but the command prompt wasn't. Using /b on the Start command made no difference.
So, the solution was - cd out of the directory into one that the batch script doesn't alter before launching the other apps via the above. Job done....
Now that just leaves me with how to close all those opened cmd windows (or those in taskmgr if I use the /b switch). That's probably a topic for another search or thread!