Batch Script to rename a particular logfile and move it to a new location. - batch-rename

I want to write a Batch Script to rename a particular logfile and move it to a new location. This script would be set as a daily job for a logfile that is generated once the job runs. Can anyone Help me .
Thanks
Anantha

Please try this, to see if it works, but you are looking for something similar, I assume:
ren file.old file.new
move /y file.new c:\location

Related

Back up script issues

Not entirely sure why our script isnt backing up, i suspect that it may be something with the Robocopy. Ik robo copy is a sys32 file but looking at the script its shown to be apart of another path then followed by it is the correct path c:\windows\sys32.
So im looking at the two "copy "lines. lines 2&3. Is that normal formatting, because the robocopy is not in that path.
if exist "C:\windows\system32\robocopy.exe" Goto SkipPrep
copy"%logonserver%\netlogon\tools\robocopy.exe""C:\Windows\system32"
copy"%logonserver%\netlogon\tools\sleep.exe" "C:\Windows\system32\
cls
:SkipPrep
This a bat file

Automic: How to implement Error-Handling in Script

I want to check a file in Unix via Automic. If the file doesnt exist it should switch the host and check if the file is there.
The problem is, that I dont now how to implement a error handling.
Everytime the script object is processing and cant find the file the skript aborted. I need a new starting point in the skript but "ON_ERROR" or ":RESTART" doesnt work.
How can I implement a logic like this: IF the script aborted due to the error-massage 'No such file or directory'start the script from here instead.
Thank you very much for your help!
Best regards
I have solved it. Use the function PREP_PROCESS_FILENAME to check if the file exists in the folder!
You have to start the task twice in the same workflow. The task-job checks if the script exists otherwise nothing to do.
if [ -f "/path/to/script" ]
then
bash /path/to/script
else
echo "Script not found"
fi
In Post-Script you can modify the state for the empty task with :MODIFY_STATE. Depend on report or returncode

Creating a bat file which executes SQL scripts

I have a folder into which a number of MSQL scripts get dropped into after each weekly sprint. For example, 10 scripts were placed into the folder today. I had to then open each script individually and run it against the applicable database. The database that it needs to be run against is in the name of the file.
e.g. [2] [CRMdata]UpdateProc.sql
The [2] represents the sequence in which it is run, so script [1] needs to be run before it.
[CRMdata] is the database I have to run it against.
This process is very tiresome, especially if there are 50 scripts to run sequentially.
I was wondering if there was an easier way to do this?
Perhpas a .bat file, which reads the filename, and executes the scripts sequentially based on the script number, as well as executing it against the database specified in the file name.
Any help is much appreciated.
Thanks.
First, when you need to run things, consider using SQL Server Job Agent. This is a good way to schedule simple things.
For a task like this, I would recommend PowerShell in combination with "sqlcmd". This command is actually the answer to your question, since it will run scripts from the command line.
However, go a step further. Schedule a job that runs once per week (or whenever you want it run). Have it consist of one step, a PowerShell script. This can then loop through all the scripts in the directory, extract the file name from the name, and run the script using sqlcmd. Along the way, also log what you are doing in a table so you can spot errors.
I don't know anything about executing SQL with MSQL. You will have to work out how to run each script against the proper database using whatever command-line utility is provided for MSQL.
I can help you with a batch file that will sort the SQL files in the correct sequence order, and parse out the name of the database.
The job is much easier in batch if the sequence numbers are zero prefixed to be a constant width. I'm assuming it is OK to rename the files, so that is what this solution does.
I also assumed you will never have more than 999 files to process. The code can easily be modified to handle more.
Some changes will have to be made if any file names contain the ! character because delayed expansion will corrupt the expansion of the FOR variables. But that is an unlikely problem.
#echo off
setlocal enableDelayedExpansion
:: Change the definition to point to the folder that contains the scripts
set "folder=sqlCodeFolder"
:: The mask will only match the pattern that you indicated in your question
set "mask=[*] [*]*.sql"
:: Rename the .sql files so that the sequence numbers are zero prefixed
:: to width of 3. This enables the default alpha sort of the directory to be
:: in the proper sequence
for /f "tokens=1* delims=[]" %%A in ('dir /b "%folder%\%mask%"') do (
set seq=00%%A
ren "%folder%\[%%A]%%B" "[!seq:~-3!]%%B"
)
::Process the renamed files in order
for %%F in ("%folder%\%mask%") do (
for /f "tokens=2 delims=[] " %%D in ("%%~nF") do (
rem %%F contains the full path to the sql file
rem %%D contains the name of the database, without enclosing []
rem Replace the echo line below with the proper command to run your script
echo run %%F against database [%%D]
)
)

batch scripting: how to get parent dir name without full path?

I'm working on a script that processes a folder and there is always one file in it I need to rename. The new name should be the parent directory name. How do I get this in a batch file? The full path to the dir is known.
It is not very clear how the script is supposed to become acquainted with the path in question, but the following example should at least give you an idea of how to proceed:
FOR %%D IN ("%CD%") DO SET "DirName=%%~nxD"
ECHO %DirName%
This script gets the path from the CD variable and extracts the name only from it to DirName.
You can use basename command:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=$(basename "$FULLPATH")
You can use built-in bash tricks:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=${FULLPATH##*/}
Explanations:
first # means 'remove the pattern from the begining'
second # means 'remove the longer possible pattern'
*/ is the pattern
Using built-in bash avoid to call an external command (i.e. basename) therefore this optimises you script. However the script is less portable.

Using xcopy to copy files from several directories to one directory

Is it possible to use xcopy to copy files from several directories into one directory using only one xcopy command?
Assuming that I have the directory tree
root\Source\Sub1\Sub2
I want to copy all .xml files from the directory root\Source including sub folder to root\Destination. I don't want to copy the folder structure, just the files.
As DandDI said, you don't need xcopy. for statement helps much. However, you don't need to state process outcome of dir command as well, this command helps better
for /R c:\source %f in (*.xml) do copy "%f" x:\destination\
By the way, when you use it from a batch file, you need to add spare % in front of variable %f hence your command line should be;
for /R c:\source %%f in (*.xml) do copy %%f x:\destination\
when you use it within a batch
Should surround %f with double quotes otherwise it will fail copying file names with spaces
You don't need xcopy for that.
You can get a listing of all the files you want and perform the copy that way.
For example in windows xp command prompt:
for /f "delims==" %k in ('dir c:\source\*.xml /s /b') do copy "%k" x:\destination\
The /s goes into all subdirectories and the /b lists only the files name and path. Each file inturn is assigned to the %k variable, then the copy command copies the file to the destination. The only trick is making sure the destination is not part of the source.
The Answer to this problem which I think is "How to gather all your files out of all the little subdirectories into one single directory" is to download a piece of software called XXCOPY. This is freely available via XXCOPY.COM and there's a free non-commercial version fortunately. One of the Frequently Asked Questions on the help facility on XXCOPY.COM is effectively "How do I gather all my files into one directory" and it tells you which switch to use. XXCOPY is though a surefire way of doing this and it comes in a .zip archive so unzipping it can be not that straightforward but it's not particularly difficult either. There is an unzipping program called ZipGenius available through the ZipGenius.it website so maybe before you download XXCOPY then download ZipGenius then it's a smallpart smalltime double wammy(!)
Might not be the exact answer but if anyone would like to do this without coding.
You can search the name of the item inside a specific folder, and then you can copy the results and later paste it into your desired folder. It will rename the same file to be the folder I believe as the prefix and then the repeated name.