Batch File to save to Folder I created - batch-file

I have a Batch file as below
for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
C:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump -uroot gs > c:\%date%\gs.sql
I create the Folder with current date and time as what I want to do, but how do I set the dump folder to insert it in the current date and time folder
The above file runs but stops when I try and direct the folder to the one I created
I get the following error
C:\Trevor>C:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump -uroot gs 04/14/2017\gs.sql 1>c:\Fri
Access is denied.

What I think you were meant to do was this:
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%DATE%") DO SET "MYDATE=%%A_%%B_%%C"
IF NOT EXIST "C:\%MYDATE%\" MD "C:\%MYDATE%"
C:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump -uroot gs > "C:\%MYDATE%\gs.sql"
Please be aware that %DATE% can change a lot from machine to machine, there is no way you can expect this to therefore work accross other machines.

Related

Checking if there already is a sub-folder bearing date in its name before attempting to create a newer version of it - using BAT script

So I got my answer to the
How to create a folder with current date in its name and copy to it all files and folders from some path on Windows 10
question, but as it was a duplicated issue and I am a newbie on this forum- it was closed and I am unable to ask additional question in similar topics
By using this How to create a subfolder based on current date? solution I was able to achieve what I wanted [thank you very much user compo]
And so this code is working A-OK:
#Echo Off
Set "sd=C:\Users\YOUR USER NAME\AppData\Roaming\Mozilla\Firefox\Profiles\fsj89244"
Set "dd=D:\Backup Copies\Firefox\Profile"
Set "ds="
If Not Exist "%sd%\" Exit /B
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
RoboCopy "%sd%" "%dd%\%ds%" /E
It creates a sub-folder with a date [on drive D] in its name and then copies to it all content from a specified path [from the drive C]. This speeds up my manual creation of backup copies
But if the script is executed again I would need it to check first if such sub-folder exists. And if yes then leave it alone and create a folder with name in date format >>YYYY MM DD v2<<; and the next time >>YYYY MM DD v3<< etc. which would stop happening when the date in the operating system would change to a next day
Set "ds="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
set ver=0
for /f "tokens=3,4 delims=v " %%a in ('dir /ad /on /b "%ds%*"') do set /a ver=%%b+1
set "newFolder=%ds% v%ver%"
md "%newFolder: v0=%"
dir /ad /on will sort the folders by name and the for ends with the last version plus one (The obvious for /d %%a in ("%ds%*") do set /a ver+=1 just counts the found folders and so will be incorrect if a folder is missing for any reason and probably will use an already "occupied" folder)
The first folder of a day will be in the format YYYY MM DD, all further folders will be in the format YYYY MM DD vX. If you want the first folder to be the same format (... v0), use just md "%newFolder%.

appending date with filename using batch scripts

I am trying to set a housekeeping for different type of file. PFB scenario.
I have below set of files in a network path(\NAS.domain.local\data\Arasan)
test.sql
test1.txt
test2.log
I am trying to rename the files as shown below and move the files to .\Archive\ directory
Test_15-12-2016_151428.sql
Test1_15-12-2016_151428.txt
Test_15-12-2016_151428.log
([Filename]_DD-MM-YYYY_HHMISS.[ext])
I have built the below batch script.
#echo ON
SET Log_Path=\\NAS.domain.local\data\Arasan
SET Script_Path=C:\Scripts
REM ###########################################REM
REM ## Do make any changes to the text below ##REM
REM ###########################################REM
cd %Script_Path%
pushd %Log_Path%
dir /b /a-d > %Script_Path%\list.txt
set HH=%TIME:~0,8%
for /F %%A in (%Script_Path%\list.txt) do (
setlocal EnableExtensions EnableDelayedExpansion
set ffname=%%A
set "fname=%%~nA"
set "fext=%%~xA"
ren !ffname! !fname!_%DATE%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%!fext!
endlocal
)
del %Script_Path%\list.txt
move *.* Archive\
popd
exit
Now my issue is it is working perfectly in my machine but when I transfer this to server and execute. It is
throwing "The syntax of the command is incorrect." Error.
And it is moving the files without renaming. As for as i know, file name and extension is not being assigned to the variable properly in below part
set ffname=%%A
set "fname=%%~nA"
set "fext=%%~xA"
Server OS: Windows server 2008 R2 Standard(SP1)
Can anyone please help me. Thanks a lot.
Here is the part of the code you should be using to get a consistent date and time output:
For /F "Skip=1" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "DTS=%%B"
Set "DTS=_%DTS:~6,2%-%DTS:~4,2%-%DTS:~,4%_%DTS:~-6%"
Echo( [%DTS%]
Pause
Just incorporate this into the rest of your codeā€¦

Storing command results in a variable in batch file

I need to search a remote directory (psexec), then store the result in a variable.
I have a directory that is filled with many other folders each containing some files I need to execute. Each computer on the network has the files I need are in different directories.
For example, I would search (using psexec) with
dir *Google_Chrome_49_0_2623_112_X64_EN_R01* /s
And I would get the result
Volume in drive C is OSDISK
Volume Serial Number is C8AD-F658
Directory of C:\Windows\ccmcache\2a
12/04/2016 01:52 PM 458 Google_Chrome_49_0_2623_112_X64_EN_R01.cab
14/04/2016 05:26 PM 69,632 Google_Chrome_49_0_2623_112_X64_EN_R01.mst
2 File(s) 70,090 bytes
Total Files Listed:
2 File(s) 70,090 bytes
0 Dir(s) 4,726,218,752 bytes free
Now I need the same batch file to cd into the directory 2a and execute a script inside.
Is there any way to store '2a' in a variable so I can cd into it?
Currently I am doing this in 2 steps, where the batch will show me the search results, then prompt for the directory:
psexec \\%PCNAME% cmd /c (cd ..\ccmcache ^& dir *Google_Chrome_49_0_2623_112_X64_EN_R01* /s)
SET DIRECTORY=%1
SET /P DIRECTORY=Enter Directory:
Problem is, I need this to be fully automated. How can I do this?
Note: I've looked at several other threads, and none of the answers seem to help me. Seemingly the results from psexec don't flow back to the local interface.
I've tried:
for /f %%a in (psexec \\%PCNAME% cmd /c (cd ..\ccmcache ^& dir *Google_Chrome_49_0_2623_112* /s ^| FIND "Directory of C:\Windows\ccmcache\") DO (
SET v=%%a
)
to try to set the entire result line to a var, but when I check the var, it's empty.
I just discovered that you can map to a network share with pushd and run the search there instead of dealing with psexec until you need it.
So my code now looks like:
pushd \\%PCNAME%\C$\Windows\ccmcache
for /f "tokens=3" %%a in ('dir *Google_Chrome_49_0_2623_112* /s ^| FIND "Directory"') DO (
set v=%%a
for /f "tokens=4 delims=\" %%m in ('echo !v!') do (set x=%%m)
)
popd
Where %x% is now the directory I'm searching for.

Write Windows batch script to duplicate folder with date appended as new file name

I am trying to write a windows batch script that allows me to duplicate a directory [foldername] with a new file name with date appended to it [foldername.date01]. But it should also check if a duplicate has already been made for current date and create a new one like [foldername.date02] and so on..
xcopy /s/e c:\source d:\target
allows me to copy but I dont know how to retrieve date and append it.
This will backup your directory structure to another drive with all copied folders named SourceFolderName_CurrentDate_BackupTime. Let me know, if any errors:
#ECHO OFF
SET "source=C:\Source" & SET "target=D:\Target"
FOR /F %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET DT=%DTS:~0,8%_%DTS:~8,6%
FOR /F "tokens=*" %%G in ('DIR /b /a:d "%source%"') DO (
XCOPY /s/e/y/b/q/r/k/i "%source%\%%G" "%target%\%%G_%DT%" )
EXIT /B
Note, wmic is supported starting from WinXP Pro, and the above wmic command outputs OS locale independent time stamp. You can adjust DT variable format in its SET statement as you wish.

output net view (batch) into text then perform action against each computer

I have been trying to find away to search for the existence of my company's software on PC's by searching hostnames on the network \\computername\c$\Program Files\Foo, and if it finds it, copy over an updated config etc.
I've seen that net view will out put all the PC's on the network, something like this:
\\DISKSTATION
\\JWLAPTOP
\\TEST
\\XP
The command completed successfully.
I was wondering if there was a way to just get the computer names in a clean list (without "command completed" etc.):
\\DISKSTATION
\\JWLAPTOP
\\TEST
\\XP
Then run some commands against it, for everything in hostnames.txt, if exist:
\\JWLAPTOP\c$\Program Files\Foo --> do copy xyz to wherever
I can take care of the part \c$\Program Files\Foo as a variable to add after the computer names in the text file.
Hope that makes sense, thanks for any tips.
edit
Been re thinking this perhaps there is a more direct way to do this....
I need to see the list of PC's on customers network.....net view is a good way of getting this info so far, but I further need to see which ones are online. Any online, query for folder and update a *.CFG file, any offline, output to text for reference.
So at the minute....
FOR /F "tokens=1 delims= " %%G IN ('net view ^|findstr /L /B /C:"\\"')
this is working great, I then made it output to a text file..
FOR /F "tokens=1 delims= " %%G IN ('net view ^|findstr /L /B /C:"\\"') DO (echo %%G>>%~dp0netview.txt)
However, the %%G echo's back \somecomputer which means I am struggling to get a new line..
for /f %%G in (%~dp0netview.txt) DO (ping etc......
to ping because of the \ before the computer name. So was wonder if we can make the list 'just" have the PC name without the \ before it.
Also this is the content of the .cfg file I need to edit...
<?xml version="1.0" encoding="utf-8"?>
<ClientConfigurationFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ServerPath>**\\server\shared\data**</ServerPath>
<ApplicationMode>Workstation</ApplicationMode>
<VRPath />
<ServicePollingInterval>0</ServicePollingInterval>
</ClientConfigurationFile>
perhaps there is a way of editing a certain section of this directly once its existence is found. \server\shared\data...in bold is what I need to update often when clients have new servers and things and involves having to go round lots of rooms to update manually. This batch could save hours upon hours of unnecessary work.
over writing the existing .cfg file is still a good way of doing it if it's too tricky or not possible directly.
Hope it makes sense, thanks for the replies!!!
Assuming none of your computer names have spaces in them.
#echo off
FOR /F "tokens=1 delims= " %%G IN ('net view ^|findstr /L /B /C:"\\"') DO (
IF EXIST "%%~G\c$\Program Files\Foo" copy "C:\folder\xyz.txt" "C:\other folder\"
)
If you want the leading back slashes stripped then use it as a delimiter just like I am using the space as a delimiter to get rid of all the extraneous NET VIEW output.
#echo off
FOR /F "tokens=1 delims=\ " %%G IN ('net view ^|findstr /L /B /C:"\\"') DO (
PING %%G
IF EXIST "\\%%~G\c$\Program Files\Foo" copy "C:\folder\xyz.txt" "C:\other folder\"
)
You could do the following:
#echo off
setlocal EnableExtensions
set "TARGET=C$\Program Files\Foo"
for /F "eol=| delims=" %%C in ('
net view ^| find "\\"
') do (
pushd "%%C\%TARGET%"
if not ErrorLevel 1 (
rem do your operations here...
copy "\your\source\path\xyz" "."
popd
)
)
endlocal
The for /F loop walks through all host names returned by net view (supposing each one starts with \\ and there are only the host names).
Since the resulting path (for instance \\TEST\C$\Program Files\Foo) is a UNC path which is not supported by several commands, pushd is used, which is capable of connecting to the given resource by establishing a temporary drive letter like Z:, and changing the working directory to its root immediately (if the command extensions are on, which is the Windows default).
The query if not ErrorLevel 1 is used to skip over the remaining commands in case pushd could not connect to the resource for some reason.
After all your operations, popd ensures that the temporary drive created by pushd is unmounted and that the former working directory is restored.

Resources