I'm trying to open various files stored on a local network server with this piece of batch code:
for /f "tokens=*" %%G in ('dir /b /a:d "\\server\directory\*"') do %%G\setup.xml
but since dir \\server\directory doesn't work so won't that piece of code.
How can I accomplish that?
Thank you in advance
You can map a drive using
net use X: \\server\directory
and then you can change to that directory using
pushd X:
You can then run your batch command on the current directory, and when you are finished and the files are no longer in use, you can delete the drive using
net use X: /delete
You write:
since dir \server\directory doesn't work
At first I was going to say that does work. But what actually works is:
dir \\server\sharename
The server has to share the directory. On the server you can use a command like:
net share public=c:\public
The you should be able to:
dir \\server\sharename
Related
Is there any way in a batch script to take a shared network path on my computer and get the absolute path to that directory?
Example:
I'm running a batch file on TESTBOX. I know the "stuff" folder on TESTBOX is shared on the network. Can I get the absolute path to "stuff"? If "stuff" were in C:\stuff then I want this:
Command = GETPATH \\TESTBOX\stuff
Result = C:\stuff
You can try to ask wmi for the share information
wmic share where "name='stuff'" get path
Any shared folders you have on your local computer can also be seen using the NET SHARE command.
H:\>net share
Share name Resource Remark
-------------------------------------------------------------------------------
C$ C:\ Default share
IPC$ Remote IPC
ADMIN$ C:\windows Remote Admin
STUFF C:\STUFF
The command completed successfully.
So if the share name is stuff you can extract the output of the NET SHARE command into a variable by encapsulating it in a FOR /F command.
FOR /F "TOKENS=1* delims= " %%G IN ('net share ^|find /I "stuff"') DO SET sharepath=%%H
I'm trying to add a step to a batch process to make sure all local files have been closed in a specific directory.
Everything I can find keeps pointing me to Net Files and Openfiles, but both of these options only close open files accessed via share (not local).
I've looked at both taskkill and microsoft's handle tool, but from what I can tell this isn't the smartest way to go about this task.
Is there an equivalent to Net Files that will close files opened locally?
Any help would be appreciated.
you might try this with handle on the command line:
for /f "tokens=2 delims=:" %a in ('handle "c:\folder"') do #for /f %b in ("%~a") do #echo handle -c %~b
Remove echo if the output looks good.
I wish to run a schedule chkdsk on all drives from a bat file via my task scheduler, that will force an automatic reboot where necessary and output the results for each drive to a text file.
I've been following an example listed here http://www.sharkyforums.com/showthread.php?302556-Automating-CHKDSK-F-R-on-all-Drives that looks to do what I need it to but it doesn't seem to be working.
in the DriveLtr.txt file I have my drives listed as
C:
D:
E:
Can anyone suggest a possible fix to get this up and running?
Many thanks
This is a barebones version (slightly modified) of the script in the link you posted.
for /f %%a in (DriveLtr.txt) do (
echo Y| CHKDSK %%a /F /R >>CheckDiskRpt.txt
)
You can add all the extra logging stuff you want to it, but that will loop over all the drives in the text file and run the chkdsk command for each of them, logging stdout to CheckDiskRpt.txt.
#ECHO OFF
REM This is a script which will run a CHKDSK against drives specified between ()
FOR %%X IN (C,D,E,F,G,H,I,J,...,Z) DO (CHKDSK %%X: > "C:\CHKDSK_LOGS\DRIVE_%%X_%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt")
My end users have Outlook pst files scattered all over their c drive. I came up with this batch file to find them and copy them to the end user's M drive.
rem **************************************************
#echo off
if not exist m:\migration mkdir m:\migration
if not exist m:\migration\pst mkdir m:\migration\pst
c:
dir /b c:\*.pst /s > m:\migration\pathdata.txt
pause
for /f "tokens=1 delims=" %%a in (m:\migration\pathdata.txt) do (copy "%%a" m:\migration\pst)
pause
BUT this paltry solution can't handle if the files have the same name, which is to be expected (archive.pst)
I was hoping to use xcopy to copy them and create their directory structure on drive M, that way files with the same names wont overwrite each other. But I have failed.
Any ideas? Thank you for helping me with this
I used this blog for help please check this one. It will search files and then copy to network location
http://tshootissues.blogspot.com
XCopy /-U c:\*.pst m:\migration\pst
The /U option will only copy files that already exist. The /-U should copy only those that do not already exist.
or
echo n|copy /-y c:\*.pst m:\migration\pst
source: http://www.dostips.com/forum/viewtopic.php?t=537
There is something you should observe with dir /b c:\*.pst /s > m:\migration\pathdata.txt
If you open your pathdata.txt or do for /f "tokens=1 delims=" %a in (m:\migration\pathdata.txt) do #echo %i you will notice you have paths like this:
C:\Documents and Settings\USERNAME\AppData\Local\Application Data\Application Data\Application Data\Microsoft\Outlook\FILENAME.pst
C:\Users\USERNAME\AppData\Local\Application Data\Application Data\Application Data\Application Data\Microsoft\Outlook\FILENAME.pst
And so on, but note it is actually the same file. This happens because dir /b c:\*.pst /s will go through folder shortcuts therefore you will have a .txt file with lots of path names pointing to the same .pst and when you xcopy you will copy the same file multiple times.
You can check this post which solves the problem then you can do the robocopy answer or modify it to your need.
Since you have multiple users and if your M drive is a network drive you could use m:\migration\%username%\pst to have it copied by user. That way if a user's pst have the same name as another user's pst you wont overwrite it and also you would be identifying them.
It is a general caution that PST file may get vulnerable threat on network driver or server. This truth is also accepted by MVP and Microsoft. So, it can use only at local drive only to avoid any problematic situation.
Hope you can help. I have been trying to resolve this for a week but not getting anywhere and can't quite piece together what I need! - My scripting skills are far from great so please forgive my naivety!
Ok, The Problem......
I have an IIS server that has multiple sites that all save their logs in a separate directory, I need to copy the logs from the last 24 hours to a local directory on my machine so I can analyse these in Log Parser Lizard (GUI Version) on a daily basis.
I can map a drive from the remote server to my local machine via a hardware VPN, so this makes things a bit easier. Using the forfiles command I can re-curse the directories to find the logs that are only a day old, and using either copy/xcopy/ or Robocopy I can set a command to copy. My problem is that the IIS logs all have the same name so my copy command just keeps overwriting the previous file, rather than creating a new file. I have tried using the %random% parameter for the file name, but this again creates one random file that is overwritten with the next file, keeping the same name instead of creating lots of randomly named files in one directory.
I know that Log Parser commands include recurse, which I have used successfully, however the format of the log is changed slightly and the GUI Lizard cannot read the data within, so this is not a solution.
My code as it stands at this time is shown below, with IP's changed for obvious reasons. Any help would be greatly appreciated!
#echo off
NET USE Q: /Delete /yes
NET USE Q: \255.255.255.255\D$\Logs
cd C:
RD /S /Q C:\Weblogs\Production
MD C:\Weblogs\Production
forfiles.exe /p Q:\ /s /m *.log /d 0 /c "cmd /c robocopy /S /XC /XN /XO #file C:\Weblogs\Production\%random%.log"
NET USE Q: /delete
exit
%RANDOM% does not work for you in this case because it does not get solved per each iteration but only once at the forfiles invocation.
You'll need either to use in FORFILES some unique identifier, maybe concatenating #RELPATH and #FNAME may work for you in case you have only one level deep recursion.
Or either replace FORFILES with a FOR loop. Inside the loop you may have more freedom to calculate a unique ID, maybe a simple counter might work for you.
Edit: see this simple code sample, to get you started
#ECHO off
SETLOCAL enabledelayedexpansion
SET destdir=C:\LOGS
SET count=%RANDOM%
FOR /R %%A IN (*.log) DO (
SET /A count += 1
#ECHO COPY %%A !destdir!\%%~nA.!count!.log
)