I have a problem about some commands in batch file with windows server 2008.
When i try to use clip command on command prompt, it works.
But when i use the clip command in a batch file and run the batch file, the clip command doesn't work. What should I do on this issue or where is my fault?
EDIT:
set wp=D:\workset\xxx\MC_DIM\exchange\
d:
cd %wp%
for %%i in (xxx_to_yyy*.zip) do (
echo %wp%%%i | clip
)
When I try to only echo I can take the full path with batch file, also when I use clip command from cmd it works but when i create a batch file and try to use it doesn't copy to clipboard. Should it be about privileges or sth ?
In my hands, your code works, except it only copies the last file listed to the clipboard. That is because each file listed gets its own pipe to a new CLIP command. Each CLIP command overwrites the result of the prior one.
You need to somehow list the full path to the files and pipe the entire result set to a single CLIP command. The easiest solution that does not use a temporary file is to put parentheses around your FOR statement. Each side of the pipe is executed in its own CMD.EXE process, with ECHO state defaulted back to on. So the command in the DO clause needs the # to suppress echoing of the command.
There is no need to include the %wp% in your output. The ~f FOR variable modifier will supply the full path.
#echo off
set wp=D:\workset\xxx\MC_DIM\exchange\
d:
cd %wp%
(for %%i in (xxx_to_yyy*.zip) do #echo %%~fi) | clip
Related
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! =)
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.
I am working on a batch script file to copy and trigger a .exe file installation
So i have coded it as follows:
set path = "c:\path_to_install_exe\"
set installationfilepath=%cd%
(this one gives, d:\installation_file_path\commands)
the installation file is present in the above path and i want to copy from my current working directory
so i gave the it as
echo xcopy "%installationfilepath%\..\install.exe" "%path_to_install_exe%"
But this does not do the necessary operation. i get d:\installation_file_path\commands..\install.exe as an output.
Can anyone please help me on this, as i am new to batch file scripting.
You could always use pushd and popd to change the directory the batch file assumes.
Like how
pushd C:\Users\Arescet\Desktop\
echo Hello world! > Hi.txt
popd
Will create "Hi.txt" with 'Hello world!' no matter where you launch the bat.
For shorter commands, you're better off setting a different fileName, in the list of default variables 'path' is already a system variable, and it is unwise to change it, as 'MC ND' stated.
For just one single file, you're better off with the copy command as it lets you indicate the path type.
I have an archive.pst file on my C: drive that I use in outlook to backup my email. But my C: is not backed up each night. So I'd like to copy the .pst file to my network drive so it will consistently be backed up. I do not want outlook to open the .pst file directly from the network drive for a variety of reasons.
Therefore I am trying to create a scheduled task that will copy my .pst file to a network location each day. The batch file below works perfectly if double-clicked. If I try to run the scheduled task, only the log file is created. Outlook doesn't close and the .pst file is not copied. I've tried running with the highest privileges but that doesn't seem to help. Any ideas would be appreciated.
cscript.exe close_outlook.vbs
::This is my VBS Script
::Set Outlook = CreateObject("Outlook.Application")
::Outlook.Quit
ping localhost > nul
set idrive="\\myserver\drive\\Outlook Files\"
set current="C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time% >> %idrive%\Log.txt
copy %current%\archive.pst %idrive%\archive.pst /y
echo End Time of Copy: %time% >> %idrive%\Log.txt
move %idrive%\Log.txt %idrive%\BackupLogs\Log.txt
ren %idrive%\BackupLogs\Log.txt %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
cscript.exe open_outlook.vbs
::This is my VBS Script
::set shell = createobject("wscript.shell")
::shell.run "outlook.exe"
EXIT
In reviewing the previous responses, I have shortened the batch file to only the code below. This works when double-clicking, but not when scheduling a task. I've also tried the same task moving the .vbs script to a network drive. Same outcome.
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\open_outlook.vbs"
1. Specify all files in a batch file executed as scheduled task with full path.
Double clicking on a batch file results usually in running the batch file with currently working directory being the directory of the batch file. But when running a batch file as scheduled task, the system32 directory of Windows is the current working directory.
Is close_outlook.vbs and open_outlook.vbs in system32 directory of Windows?
I don't think so. Replace in batch code below Path to\Script File twice by the right path.
2. Assign string values with space to environment variables right.
variable=value is the parameter for command set. With
set idrive="\\myserver\drive\\Outlook Files\"
you assign to variable idrive the value "\\myserver\drive\\Outlook Files\" with the double quotes included. This results on expansion of
echo End Time of Copy: %time% >> %idrive%\Log.txt
in the command line
echo End Time of Copy: 19:21:53 >> 1>"\\myserver\drive\\Outlook Files\"\Log.txt
and this is not right, isn't it.
Correct is:
set "idrive=\\myserver\drive\Outlook Files"
I removed also second backslash after drive and the backslash at end of folder path.
As the environment variable contains now the path with space(s) without double quotes, the double quotes must be added where the value of the environment variable is used concatenated with a file name, see batch code below.
There is one more reason why using "variable=value". A not visible trailing space at end of the line with command set in batch file is also appended to value of the environment variable if double quotes are not used or used wrong. Read this answer for details about correct assignment of string values to environment variables.
3. Define the wait loop using command ping better.
The command
ping localhost > nul
produces a wait. But it is better to use something like
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
as now the wait is determined with exactly 3 seconds.
4. Do not insert a space left of redirect operators > or >> for stdout.
Why this should not be done was explained by me here in detail.
5. Avoid environment variables not defined in batch file itself or in system account.
Your batch file uses only environment variables defined in batch file itself. So this advice is here not really needed.
However, many batch file working fine on double click but not on running as scheduled task fail because the batch file depends on environment variables like PATH or others which are related to current user account. It is safe to use environment variables which exist for all accounts like SystemRoot.
Reworked batch code
Here is your batch file with the appropriate changes whereby the paths of the two *.vbs files must be set correct by you before the batch file (hopefully) works as scheduled task.
%SystemRoot%\System32\cscript.exe "Path to\Script File\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
set "idrive=\\myserver\drive\Outlook Files"
set "current=C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time%>>"%idrive%\Log.txt"
copy /B /Y /Z "%current%\archive.pst" "%idrive%\archive.pst"
echo End Time of Copy: %time%>>"%idrive%\Log.txt"
move "%idrive%\Log.txt" "%idrive%\BackupLogs\Log.txt"
ren "%idrive%\BackupLogs\Log.txt" %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
%SystemRoot%\System32\cscript.exe "Path to\Script File\open_outlook.vbs"
set "idrive="
set "current="
I am attempting to use a looping batch file to launch the CMD app ssdeep and pass a file argument to it, then keep the ssdeep window open to copy a chunk of output to the clipboard I have the following code:
#ECHO OFF
:start
SET /p filetohash= What file would you like to fuzzy hash?
START C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash%
PAUSE
goto start
This allows me to run the batch file, which I can then drag and drop a file to hash into the CMD window. Upon hitting return, the ssdeep CMD window appears for the moment it takes to hash the file, then closes. This leaves me with the 1st window generated by the batch file, that is requesting a key press.
I want to have the 2nd CMD window stay open so I can copy the hash out. Similar to the PAUSE I used in the batch file, but I need it to apply to the 2nd CMD window created.
I'm not exactly sure how to search for this information. I have searched info on batch files. I used these resources to get thus far:
https://superuser.com/questions/582095/how-to-create-a-batch-file-that-will-run-cmd-and-a-exe-with-parameters
and
Batch files : How to leave the console window open
Thanks in advance,
PTW-105
use the /b switch to the start command - see if that does what you need (leave the pause in).
start "" /b C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash%
The empty double quotes protect the start command if you ever add quoted items in the commands.
Try this command to create hash.txt on your desktop - remove the pause - it should contain the information if it gets printed to STDOUT. It can be parsed to extract just the hash: if you add that information to your question it should be in a format that we can read and see how to parse it.
start "" /b C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash% >"%userprofile%\desktop\hash.txt"