Execute a copy command using a batch file by opening CMD - batch-file

I'm creating a batch file on my desktop which has a simple code:
%SystemRoot%\system32\cmd.exe
This will open up Command Prompt.
Then I need to copy paste following code into the command prompt.
for /r "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\Non-Conformance Reports\" %i in (NCR*.pdf) do copy "%~fi" "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\NCR Log Tracking\PDF Destination from DOS\" /Y
The above command simply copies and pastes PDFs from one directory to another directory.
Is there a way to write the entire thing into a batch file?
Desired output is:
A Desktop Icon of a BAT File.
Double clicking on it will do two things: Open up Command Prompt and Executes the Copy command.
Closes the Command Prompt once done
Once that's done, I can simply use Windows Task Scheduler to run this Bat file everyday at 5:00 AM.
All the helps are appreciated. Thank You.

Create a batch file on the desktop and use the following code...
#echo off
for /r "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\Non-Conformance Reports\" %%i in (NCR*.pdf) do (
copy "%%i" "Z:\QCQA\Main QCQA Files\QA System Files\Nonconformance\NCR Log Tracking\PDF Destination from DOS\" /Y
)
Double Click on the batch file anytime you want to run it.
That's it! =)

Related

Create a batch file to run an .exe with one argument

I want to create a batch file that a user can run ... in the batch file I want to run an exe with one argument.
Here is what I have today:
#echo off
c:\
cd "C:\Program Files (x86)\App Location\App34\"
start HelperSetup.exe -arg
When I run that it opens up the cmd window and says the path cannot be found but i know for 100% it is the correct path.
I have tried to also pass in the string in a one line but no joy
"C:\Program Files(x86)\App Location\App34\HelperSetup.exe -arg"
I have tried to also pass in the string in a one line but no joy
When you want to also pass in the string in a one line you need to set the closing quote at the end of the path like this:
"C:\Program Files(x86)\App Location\App34\HelperSetup.exe" -arg
A much simpler approach for your batch script is to use the following command sequence
start /d "C:\Program Files (x86)\App Location\App34\" HelperSetup.exe -arg
This way, you don't need to change the drive and the cd command at all.

Why is xcopy command not working for copying a bitmap file into a subdirectory of Windows system32 directory?

I have a problem with my batch file. I want to copy some files using xcopy, but it isn't working.
#echo off
md %systemroot%\system32\hello
md %systemroot%\system32\wlppr
xcopy /y "%cd%\qwerty.bmp" "%systemroot%\system32\hello"
xcopy /y "%cd%\qwerty2.bmp" "%systemroot%\system32\wlppr"
The folders create succesfully, but when Windows tries to copy files this message appears in CMD:
File not found - qwerty.bmp
0 File(s) copied
File not found - qwerty2.bmp
0 File(s) copied
And yes, I tried to run the file with administrator privileges.
What am I doing wrong?
By double clicking on a batch file the Windows command interpreter cmd.exe is executed with using implicitly option /C to close the console window automatically and terminate the command process when batch processing is finished after successful execution or an exit because of a syntax error. This behavior is not good for debugging a batch file not working as expected. Better for debugging a batch file is opening a command prompt window resulting in running cmd.exe with using implicitly option /K to keep open the console window and let the command process continue running after executing a command like a batch file (not containing exit without /B) to view all error messages output during execution of the batch file.
Double clicking on a batch file results usually in executing the batch file in the directory of the batch file. For that reason the current directory is the directory of the batch file.
But if the batch file is stored on a network share opened in Windows Explorer with a UNC path, Windows command interpreter outputs usually a message that the current directory is set to %SystemRoot% before executing the first command line from batch file. This behavior can be turned off via a special Windows registry value.
And if the batch file is executed with right clicking on it and using context menu option Run as administrator, the Windows command process is started with %SystemRoot%\System32 as current directory resulting in executing the batch file with the Windows system directory as current directory.
So it is advisable to write this batch file to work independent on current directory. A good idea would be to put the two bitmap files to copy into same directory as the batch file and use %~dp0 to reference the directory of the batch file independent on what is the current directory on execution of the batch file. %~dp0 references drive and path of argument 0 which is the name of the batch file. Run call /? in a command prompt window for details on referencing batch file arguments.
The command XCOPY creates automatically the entire folder tree on copying 1 or more files if the target string ends with a backslash making it clear for xcopy that the target is a folder name and not a file name.
So the commands to use are:
%SystemRoot%\System32\xcopy.exe "%~dp0qwerty.bmp" "%SystemRoot%\System32\hello\" /Q /Y >nul
%SystemRoot%\System32\xcopy.exe "%~dp0qwerty2.bmp" "%SystemRoot%\System32\wlppr\" /Q /Y >nul
The folder path of batch file referenced with %~dp0 always ends with a backslash. Therefore no additional backslash is specified in both source file name strings.
Run in a command prompt window xcopy /? for help on this command.

Copy file from directory to another using cmd command

I want to copy a file from sharing folder to another using cmd command ".bat file"
the below code works normally with most files except MS Access files the "accde" extension so, please advise.
MKDIR "\\192.168.0.110\Attendance Sheet\JTA\events\events_media\SysFile"
XCopy /y/z "\\192.168.0.110\Attendance Sheet\JTA\events\events_media\db.accde"
"\\192.168.0.110\Attendance Sheet\JTA\events\events_media\SysFile"
Run this code to have the command execute in the background so that you can continue to work from the command line while it is running:
start /min xcopy /y /z "sourcefile" "destination"
Actually, the command is running in a minimized window of it's own - you can see it popping up in the taskbar. But from a user's perspective it's the same as running "in the background".

How to run a cmd command from bat file?

I need to run this command in the command prompt automatically:
msiexec /i "My application"/qn
How can I write a batch file to do this?
Batch files are, for all intents and purposes, just lists of cmd commands. Just take that command, paste it into notepad, and save it with a .bat extension.
It's generally considered good practice to start the batch script with the line #echo off so that the lines of the script don't display as they're being executed.
#ECHO OFF
msiexec/i "My application"/qn
To run the batch file when the VM user logs in:
Drag batch file itself to Start - All Programs - Startup. Now when you login as that user, it will launch the batch file.
you may find the Batch HowTos helpful.
Examples: see Link
Install:
msiexec /i "C:\Install\ss64app.msi"
UnInstall:
msiexec /uninstall "{5AFF6499-63BA-4A36-83B2-8D256404AC3D}" /log "C:\install\ss64app.txt"
Autorun
1. create text file
2. edit with Notepad
[autorun]
icon=drive.ico
open=launch.bat
open=launch.bat
[autorun]
open=\folder1\runinstallers.bat
open=\folder2\PLSetup_2.exe
3. save the file as Autorun.inf
4. copy the Autorun.inf to the root folder of your CD-ROM

batch file to copy files to another location?

Is it possible to create a batch file to copy a folder to another location everytime I login, or when the folder is updated?
It could be written in VB or Java as well if not an easy solution.
Any ideas?
Two approaches:
When you login: you can to create a copy_my_files.bat file into your All Programs > Startup folder with this content (its a plain text document):
xcopy c:\folder\*.* d:\another_folder\.
Use xcopy c:\folder\*.* d:\another_folder\. /Y to overwrite the file without any prompt.
Everytime a folder changes: if you can to use C#, you can to create a program using FileSystemWatcher
#echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
Open Notepad.
Type the following lines into it (obviously replace the folders with your ones)
#echo off
rem you could also remove the line above, because it might help you to see what happens
rem /i option is needed to avoid the batch file asking you whether destination folder is a file or a folder
rem /e option is needed to copy also all folders and subfolders
xcopy "c:\New Folder" "c:\Copy of New Folder" /i /e
Save the file as backup.bat (not .txt)
Double click on the file to run it. It will backup the folder and all its contents files/subfolders.
Now if you want the batch file to be run everytime you login in Windows, you should place it in Windows Startup menu. You find it under: Start > All Program > Startup
To place the batch file in there either drag it into the Startup menu or RIGH click on the Windows START button and select Explore, go in Programs > Startup, and copy the batch file into there.
To run the batch file everytime the folder is updated you need an application, it can not be done with just a batch file.
It's easy to copy a folder in a batch file.
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And you can add that batch file to your Windows login script pretty easily (assuming you have admin rights on the machine). Just go to the "User Manager" control panel, choose properties for your user, choose profile and set a logon script.
How you get to the user manager control panel depends on which version of Windows you run. But right clicking on My Computer and choosing manage and then choosing Local users and groups works for most versions.
The only sticky bit is "when the folder is updated". This sounds like a folder watcher, which you can't do in a batch file, but you can do pretty easily with .NET.
Batch file to copy folder is easy.
xcopy /Y C:\Source\*.* C:\NewFolder
Save the above as a batch file, and get Windows to run it on start up.
To do the same thing when folder is updated is trickier, you'll need a program that monitors the folder every x time and check for changes. You can write the program in VB/Java/whatever then schedule it to run every 30mins.
robocopy yourfolder yourdestination /MON:0
should do it, although you may need some more options. The switch at the end will re-run robocopy if more than 0 changes are seen.
#echo off
cls
echo press any key to continue backup !
pause
xcopy c:\users\file*.* e:\backup*.* /s /e
echo backup complete
pause
file = name of file your wanting to copy
backup = where u want the file to be moved to
Hope this helps
#echo off
xcopy ...
Replace ... with the appropriate xcopy arguments to copy what you want copied.

Resources