I need help writing a batch file to update templates on a database. Basically, all our clients have their own folder, with multiple templates inside. Due to the computer illiteracy of my office (sigh), there's no real better way to fix this. However, I need a way to update those templates in a batch. For instance
\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD
is updated. I then need to copy it to:
\\SERVER\Client Files\Client 1\Correspondence;
\\SERVER\Client Files\Client 2\Correspondence;
...etc. Essentially, I need to copy to \\SERVER\Client Files\\*\\, and I need to make it a batch file that I can train someone else to use whenever I leave this job. How can I do that?
Thanks.
The new versions of Windows (7 and 2008 Server R2) have the robust file copy tool (robocopy). This can be installed on XP and 2003 also be installed using the Resource Kit. Essentially, robocopy gives you a command-line directory mirroring tool that could help you accomplish what you're trying to do. Simply place robocopy commands into a batch file (/MIR = mirror directory contents /XJ = ignore junctions) :
robocopy <source_dir> <destination_dir> /MIR /XJ
You didn't indicate which operating system you are working under. Let me guess its windows. My DOS BAT file knowledge is limited, but you
could try creating a BAT file with something like:
set Src="\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD"
set DestA="\\SERVER\Client Files\
set DestB=\Correspondence;"
FOR /F "delims=" %%i IN (distribution.txt) DO copy %Src% %DestA%%%i%DestB%
and then create a distribution.txt file like:
Client 1
Client 2
Running this BAT file will read the distribution.txt file and issue a copy command for each line in it. As follows:
COPY "\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD" "\\SERVER\Client Files\Client 1\Correspondence;"
COPY "\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD" "\\SERVER\Client Files\Client 2\Correspondence;"
But there must be a better way!!!!
You can get more help on the FOR command by typing help for at the DOS prompt.
If you don't like the idea of having to build/maintain the distribution.txt file, you could play with using DIR /A:D /B "\\SERVER\Client Files\*" to drop a directory listing into a temporary file, then use it as input to the FOR loop.
Related
Hi Sorry to ask another question regarding robocopy but by its very nature it is quite diverse so I need to be clear on what I am trying to achieve.
Scenario.
Moving from a Nas to a windows file Share , Completely New ACL's will be created and inherited from the new share already setup . SO this is a straight file and folder copy with mirroring.
I decided to use powershell to run the command - it felt right at the time and achieved the desired result on a few shares .
I mapped the drive and ran the following .
robocopy z:\"Dept Folder 1"\ E:\Shares\deptdata\"New dept folder" /e /LOG+:file /v /TEE
robocopy z:\"Dept Folder 2"\ E:\Shares\deptdata\"New dept folder2" /e /LOG+:file /v /TEE
The files and folders copied as expected inheriting the new ACL's The first Job finished and then moved on to the second copy and again completed successfully.
What I would like to do now on some of the larger shares is incorporate the mir command as I need to keep a constant mirror on all folders until such time as I have tested the new ACL's and I have mapped the new drive via GPO.
My First Question knowing the behaviour when having batched commands is this .
When using the Mir command will the first command in the batch continue to mirror
the share and move onto the next Mirror command and so forth. or will it just sit their mirroring the first command and not move onto the second?
If it wont move on to the next command how do I achieve that is it possible? would i have to run multiple mirror commands at the same time and not batch them up in one?
When the Mir command is running if I add new files into the New destination will those files get deleted, or is it only mirroring the source and doesn't care about new files in the destination?
Where would I specify the MIR command?
I'm trying to write a small bat script to put on my teams desktops and allow them to update their personal macro file with mine when ever I push out an update or have created new tools.
I have the following
xcopy "O:\abc Supply chain\Supply Chain Team\David Peters\Excel\Macro File" "%AppData%\Roaming\Microsoft\Excel\XLSTART\" /y
under CMD is says 1 files copied yet there isn't anything in the XLSTART folder.
can you pleased tell me what I'm doing wrong
many thanks
Not sure about your configuration, but for me the Roaming folder is already included in the value of the %AppData% variable.
"%AppData%\Roaming\Microsoft\Excel\XLSTART\"
^......^
So, probably you should use "%AppData%\Microsoft\Excel\XLSTART\"
I came across a way to convert my .bat with dependencies on tool to an .exe file. However when I try using the script and run the .exe created, I always getting an error. Seems I modified the script incorrectly.
Anyone can help, please?
Here's the code with my modifications:
#ECHO OFF
ECHO Make EXE From BAT
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.
REM Usage:
MakeExeFromBat BatFileToConvert -bat MyProgram.bat
REM
REM Required Parameters:
BatFileToConvert -save MyProgram
REM Source batch file to use to produce the output Exe file.
REM
REM Optional Parameters:
IncludeFile -include Tool.exe
REM Additional files to include in the Exe file.
REM You can include external tools used by the batch file so they are available on the executing machine.
SETLOCAL
REM Configuration (no quotes needed):
SET PathTo7Zip=C:\Desktop\
REM ---- Do not modify anything below this line ----
SET OutputFile="%~n1.exe"
SET SourceFiles="%TEMP%\MakeEXE_files.txt"
SET Config="%TEMP%\MakeEXE_config.txt"
SET Source7ZFile="%Temp%\MakeEXE.7z"
REM Remove existing files
IF EXIST %OutputFile% DEL %OutputFile%
REM Build source archive
ECHO "%~dpnx1" > %SourceFiles%
:AddInclude
IF {%2}=={} GOTO EndInclude
ECHO "%~dpnx2" >> %SourceFiles%
SHIFT /2
GOTO AddInclude
:EndInclude
"%PathTo7Zip%\7za.exe" a %Source7ZFile% #%SourceFiles%
REM Build config file
ECHO ;!#Install#!UTF-8! > %Config%
ECHO RunProgram="%~nx1" >> %Config%
ECHO ;!#InstallEnd#! >> %Config%
REM Build EXE
COPY /B "%PathTo7Zip%\7zsd.sfx" + %Config% + %Source7ZFile% %OutputFile%
REM Clean up
IF EXIST %SourceFiles% DEL %SourceFiles%
IF EXIST %Config% DEL %Config%
IF EXIST %Source7ZFile% DEL %Source7ZFile%
ENDLOCAL
This doesn't really convert a bat file to an exe. It just creates a selfextracting archive (exe) which contains the bat file. On execution it extracts the file to a temporary folder and runs it from there. You can even extract the bat from the exe just by using 7zip/rar/winzip or any other archiver.
If you want to convert a bat to an exe for real you should use one of the tools from the web (like this one: http://www.f2ko.de/index.php?lang=en) or concider using a simple script language like AutoIt.
If you pick the second, you can simply execute your bat code with Run("put your bat code in here") and you can compile your script to a "real" exe file.
For an alternative approach, you can basically do the same thing as described in the accepted answer (making a 7z-SFX) with WinRAR. That way, you can also do it with a GUI, and I will try to add some more useful information.
Actually, you can also use the latter approach to generate portable applications and it also works with "converting" every runnable (or openable) file into an .exe.
If you need that "portability hack", you should unpack your .exe or .msi installer with Universal Extractor. Details can be found in this Article, Step 1 to 4. Newer Versions of 7zip or WinRAR also come with comparable features.
Now you add all needed files to the archive. In the easiest case, this is just your .bat script or whatever file you want to "convert" into an .exe applivation. (Step 5 here)
Steps 6 and 7 are just some Settings for the SFX-Archive, 8 is the interesting one, as you select what you actually want to run there. Input the name of your (.bat-)file.
Step 9 lets you select where to unpack to - you do this setting manually and programmatically in the MakeExeFromBat.bat-script.
After this process you created a Portable App in SFX archiever form, enjoy
The word "converting" was put into quotation marks, because running that .exe actually works like this:
The contents of the (SFX-)EXE file are extracted from the "archive part" to a directory as the specified temp directory.
( The config file generated by the script is read. )
The file, that was previously contained in the EXE file and then extracted, is now executed in a new window.
a) This file could besides a .bat be anything - as e.g. an image, a MP3 or a video
b) or also a Python Script (of course your OS needs to know how to deal with that file.
Once finished, the temp files are removed.
You can also derive some limitations from that. If you have a .bat that needs the content of the working directory, you will have a problem. (Say, a batch that renames all files in the current dir from 1 to n.) In some cases that can be dealt with by adding all needed files to the archive too. On Windows Vista and all newer OSes, you might encounter a message box after the script is run. After selecting ‘This program installed correctly’, the message box will not be displayed in the future for this file. Because the EXE file launches in a new window, the typical way of logging output (using the > char) will not work as expected. In order to log the output, you would need to handle this natively in your source script.
All references were already linked, but once again: Big credit goes to Jason Faulkner for providing the Article and 7zip-Approach, binbert for the WinRAR-SFX Solution (which is as hinted much more versatile -> portability) and some credit to creative8 for finding the two and the article comparing them.
Actually, I was develping another solution using AutoHotkey. In my case, I just want to be able to add my .bat to the windows start menu - but the options are not limited to that.
The script itself is just a oneliner and .AHK is easily converted to .exe (I used v1.1.33.09):
run % SubStr(A_ScriptName, 1, -4) ;// run also has the option to run your file minimized or hidden, see the source 2 below
Source 2
What it does is taking its own name, removing the .ahk or .exe respectively (the last 4 characters, hence -4) and running excactly that. Usage could not be easier: you have a runme.bat, so you rename the program I provide to runme.bat.exe. Say you want the .exe to open an image.png - guess what, rename it to image.png.exe. You get the gist - that's it. It dynamically checks its name to find what to run. In my opinion, this is not much less mighty than "unpacking the .bat and then run it", but (again imho) it is much more elegant.
Use it as you wish, I should probably start a public github page or so.
I have a user, which couldn’t get along with AutoCAD so he switched back to InterCAD. He’s not too computer literate so now when he tries’s to open a DWG file (AutoCAD native file extension) by double clicking it he’s register settings look for the AutoCAD program to open it.
I know that we can tweak the register settings for a .dwg file to open the file automatically with InterCAD rather then AutoCAD.
I’m not too un-familiar with tweaking the registry keys and when I do I like to automate this using batch script.
What is the best procedure to do this, I'm namely worried I will miss a key or is the following the only key I need to tweak
HKEY_CLASSES_ROOT\.dwg\OpenWithprogids
The extension to execute Intercad in Intercad.exe
How do I successfully achieve my desired result?
The key you need to edit will be this one:
HKEY_CLASSES_ROOT\dwgfile\shell\open\command
That controls the program the file opens with.
To do this in a batch file use this:
reg add HKCR\dwgfile\shell\open\command /v "" /d "programpath.exe" /f
Hope this helps.
What are batch files useful for? They just seem to be used to make viruses and other things...but it seems like shell scripting to me.
Whats the uses for batch files?
From Batch file article at Wikipedia:
Batch files are useful for running a
sequence of executables automatically
and are often used by system
administrators to automate tedious
processes. Unix-like operating
systems (such as Linux) have a similar
type of file called a shell script.
A simple example:
for /f "tokens=* delims=" %%i in ('dir /s /b /a:d *svn') do ( rd /s /q "%%i")
If you save the above line in a file called ClearSVNFolders.bat and after that execute a double click you'll delete every folder named svn that resides inside a root path...
You automated the whole process. You could easily spend hours doing the above task if you had a deep root directory, that is, one containing thousands of folders. :)
Batch files are the Windows equivalent of a Unix shell script. So you can use them to automate things.
You could use them for shell scripting. :-P
Of course, they kind of suck at that, compared to bash (or perl/python/tcl). But if you're on Windows, it's a one-horse race unless you want to install cygwin or msys and battle with Unix/Windows incompatibilities.
Batch Files are extremely useful. They are super easy to learn as well. you can make them do things on startup like say that a program wants to open itself and wont close even from taskman.exe you can force it to shutoff without warning.
or you could make games and ineractive things like i like to do.
i have a Messenger that i made with fully customizable colors and accounts with account management and servers.
But you probably dont trust me enough for you to download it.
But yea they are pretty useful.
Batch file is "a computer file containing a list of instructions to be carried out in turn."
We have been studying since childhood that computer is a dummy machine and this is a method of instructing a dummy machine.
For example :-
If you want to instruct the system to create a folder with random name then type ,
#echo off
md %random%
Creating Batch files enables you to execute several line of CMD commands in a single file.
For example :-
#echo off
md %random%
tasklist
Pause
The entire purpose of a Batch script is to execute several DOS commands in sequence:
echo Hello!
set var=7
echo I just made var=%var%!
pause
It was invented in MS-DOS for user simplicity to execute things they did all the time, the most notable thing being "AUTOEXEC.BAT" which started once the command interpreter started, people would add things like:
echo Welcome to my computer!
or
cd C:\Games\
To make it quicker to access their games or whatever they needed.