Close all locally opened files in a given directory - windows

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.

Related

Windows batch For loop not working on one server, working on other

I've got a Windows 2008 Server (non-R2); I was trying to modify an attribute (/AppPool/maxProcesses) based on a list of application pools.
So I did this:
set appcmd=c:\Windows\system32\inetsrv\appcmd.exe
for /f %p in (c:\apppools.txt) do #%appcmd% set apppool %p /processmodel.maxProcesses:2
No output. Also, this was all interactive, not a batch file.
Usually, it should display the AppPools, and the fact that it's setting the attribute, or an error, in case of typos.
I then went on to verify why wouldn't it output
for /f %p in (c:\apppools.txt) do #echo %p
Nothing again.
I go to another Windows 2008 (they are actually sysprepped, so considered to be identical. Both commands work!
Has anyone seen anything like this, or at least similar?
I was thinking this server might need its sleep (a reboot) but that might only be possible over the weekend, since it is a server.
Thanks for any suggestions,
If you are running this from inside a batch file, you will need to escape the % in your for loop variable with another %.
for /f %%p in (c:\apppools.txt) do #echo %%p
edit
Sorry, I just noticed now that you said it was interactive and not batch.
It could be that the file is hidden on one system and not the other, or if it is hidden on both, that hidden files are processed differently on the 2 systems.
Try using a combination of the comments already provided.
for /f "delims=" %p in ('type c:\apppools.txt') do #echo %p

Batch File Iterating through files on a local network server

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

Windows xp: roll my own desktop tool to copy list of directory names

Many times I have found the need to copy directory names from one directory into another, creating a list of empty directories in the later.
I have achieved this task using the following command:
for /F "usebackq" %i IN (`dir /b C:/backups/sites/24/01/2012`) DO makdir C:\fabio_temp\test\%i
Now i would like to create a reusable and friendly tool so that i don't have to be typing this all the time on the command line.
Example of what I want in pseudo-language:
$dir = PROMPT('Type in the name of the directory containing the list of directories to clone:');
$dir_dest = PROMPT('Type in the destination directory:');
FOREACH LIST_DIRNAMES($dir) AS $dirname DO
MKDIR CONCAT($dir_dest,$dirname)
ENDFOREACH;
Then, it would be nice to have this function appearing in the right-click context menu. It doesn't matter what language is going to be used for this. It could be vbscript, or whatever, I don't know.
Thanks to the tutorial that #AlbertoSolano suggested, I was able to write a simple script named mkdirs_from_list.bat with the following content:
for /F "usebackq" %%i IN (`dir %1 /b`) DO mkdir %2\%%i
That does exactly what I was asking for, and, to make things easier, I'm adding the script to the path environment, so it can be invoked like this on the command line:
mkdirs_from_list C:\dir_to_list C:\dir_dest
I wanted something more friendly, like a prompt popping up, being able to browse the directories, or something alike, but I think this will suffice.

Batch or VB Script to copy logs from multi directories to one directory and rename to avoid conflicts

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
)

DOS command to replace all instances of <filename>.config

i have an edited version of a config file specific for my machine.
i have the same config file in multiple different directories in my development folder.
i want to, in a single bat file, replace all instances of this file with my edited one.
So in pusedo code:
Take C:\edited.config and copy to C:\Projects\ /s wherever original.config is found
i want the final file to have the name of original.config, not edited.config
so i am guessing i need some combination of a FOR, a rename and copy or something like that
is this easier to do in Powershell?
can anybody help?
Thanks
I blogged about this a little bit ago at http://jamesewelch.com/2008/05/01/how-to-write-a-dos-batch-file-to-loop-through-files/
I think your solution will look something similar to (below is untested but used to show general idea)
for /f %%a IN ('dir /b *.config') do copy c:\master.config %%a
There's probably a switch there on the copy to suppress file overwrite warnings, but I don't remember what the switch is. This will copy your master.config and overwrite your local file (variable of %%a).
I'm amazed what DOS batch file experts make work. Since I'm not one of them, I take an approach that's pragmatic for me. It might work for you as well.
Get a list of destination folders
C:
Cd\
Dir original.config /s > original.bat
Edit original.bat in your favorite text editor (I like Notepad++)
Search for "original.config" and replace with "" (empty string)
Insert the text "Xcopy C:\edited.config " at the front of each line
Proof-read the result to be sure it's what you want. If you're not sure put an "Echo " in front of each line for a dry run.
Run the batch file.
#echo off
C:
cd \Projects
FOR /F "tokens=*" %%G IN ('DIR /B /S original.config') DO xcopy /y c:\edited.config %%G

Resources