I am trying to copy a file to a folder using the following batch script
echo xcopy \\path1\file.txt \\path2\backup
However, I get the following error:
UNC paths are not supported. Defaulting to the windows directory.
Is there a simple solution for this?
For Single file copying, simply use copy
copy /Y \\path1\file.txt \\path2\backup
Other ways to create a network share:
for /f "tokens=2" %i in ('net use * \\server1\folder\') do set src=%%i & goto :continue
:continue
for /f "tokens=2" %i in ('net use * \\server2\backup\') do set dest=%%i & goto :cp
:cp
copy %src%\file.txt %dest% /Y
net use /d %src%
net use /d %dest%
The above will only work if you have credentials setup already.. if not, you can do it as:
net use \\server1\IPC$ /user:username password
Related
I am trying to set up a batch program that will go to the different computers on my network (from .txt file) and then delete files from the users on the that PC, and then empty the recycle bin. I've got the second part working so I can delete files from multiple users on a PC, but I can't get it to look at other PC's. I was hoping someone might point out what I'm missing here. Here is what I have so far:
#ECHO off
Setlocal EnableDelayedExpansion
FOR /F "delims=" %%i IN (test.txt) DO (
for /f %%a in ('dir /B /AD C:\Users') do (
REM for /f "tokens=*" %%a in (userlist.txt) do (
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Lotus\Notes\Data\workspace\logs"
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Google\Chrome\User Data\Default\*"
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files"
)
RD %systemdrive%\$Recycle.Bin /S /Q
)
pause
Anybody got any pointers?
You need to use UNC paths.
\\server\sharename\folder\file.ext
You can get a list of computers with
for /f "skip=3" %A in ('net view ^| findstr /C:"\\"') do Echo %A
All computers have a share for admins only called C$ that is the C drive (and D$ etc). $ makes it hidden. So
for the local computer via UNC
del /S /Q "\\127.0.0.1\C$\User\username\AppData\Local\Lotus\Notes\Data\workspace\logs"
Also there is no point to If Exist, just delete it - it will work or not. If you care if it worked or not you test the return value If errorlevel 1 echo error. You are causing extra disk access and network traffic. In programming we do and test not test and do.
You can also run a batch on the other computer.
wmic /node:"#Computerlist.txt" process call create "c:\\batchfile.bat"
Note \\ in paths. C:\\batcfile.bat is C: on the remote computer. This allows you to only use one for loop in your batchfile. You copy the batch file with copy. Net View can generate the computer list although you have to remove \\
for /f "tokens=1 delims=\" %A in ('net view ^| findstr /C:"\\"') do Echo %A
I wan to write a batch script that can delete files from a specific folder for a bunch of users on that same computer. Not sure how this could work but I have this below of the idea of how it could work. I want to remote share in a pc, then navigate to all folders in the users folder then delete a text file in the specified path. How do I make this work?
#echo on
cd \\computername\C$
for /f "usebackq" %%m in (`dir /b \\computername\C$\users`) do (
del \\computername\Users\%%m\AppData\Local\folder\folder\Log\*.txt"
)
PAUSE
or
#echo on
psexec #pclist.txt -s cmd for /f "usebackq" %%m in (`dir /b \\computername\C$\users`) do (
del \\computername\Users\%%m\AppData\Local\folder\folder\Log\*.txt"
)
PAUSE
In both examples, you are looping over the C$ share but not using that in your del command.
Try this:
#echo on
for /f "usebackq" %%m in (`dir /b \\computername\C$\users`) do (
del "\\computername\C$\Users\%%m\AppData\Local\folder\folder\Log\*.txt"
)
PAUSE
Since most native cmd commands are not capable of handling UNC paths properly, I would go for the following (assuming that the given UNC path is accessible):
#echo on
pushd "\\Computername\C$\Users"
for /F "usebackq" %%M in (`dir /B "."`) do (
del ".\%%~M\AppData\Local\folder\folder\Log\*.txt"
)
popd
pause
pushd creates a temporary drive if a UNC path is given and makes its root the current directory. popd restores the previous state.
I found this post in regards to wildcards in directories. However, my problem is that I have multiple varying directory names between my static directories. For example:
O:\123456 Client Name\Spring\Shoot 1 12345\01 MHP 01\PlCache\GreenScreen\
O:\121212 Someone Else\Spring\Shoot 1 21212\01 MHP 02\PlCache\GreenScreen\
The above link only allows for one wildcard directory instead of muliples.
Within these GreenScreen folders, I have .png files I wish to delete. How would I write a .bat file that deletes *.png within O:\ *\GreenScreen\ ?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:"
FOR /f "tokens=1*delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.png" '
) DO (
SET "targetpath=%%~pa"
IF "!targetpath:~-13!"=="\GreenScreen\" ECHO DEL "%%a"
)
GOTO :EOF
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
I've changed to starting directory to U: to suit my system.
Here's a simpler option - it also echos the del commands to the screen until you remove the echo keyword.
#echo off
for /d /r "o:\" %%a in (GreenScreen*) do if /i "%%~nxa"=="GreenScreen" echo del "%%a\*.png"
I've written a batch file to copy files from one server to another, however, i need to be able to rename the file just copied to contain the folder path. The code i have come up with to do the job is:
ECHO OFF
SETLOCAL EnableDelayedExpansion
set include=*.log
FOR /L %%i IN (1,2,3) DO (
net use i: \\my-server%%i\d$\IISLogs
FOR /R i:\ %%G IN (%include%) DO (
XCOPY %%G D:\ServerLogsAndBackups\IIS\w%%i\
)
7z a -t7z D:\ServerLogsAndBackups\IIS\w%%i\files%%i.7z *.log -mx9
net use i: /delete
)
The file would be coming from something like:
i:\w3svc98435783475\ex110430.log
And what I want to do is copy it into D:\ServerLogsAndBackups\IIS\w1\w3svc98435783475_ex110430.log. I'm unsure how to get the directory path on the remote to put into the filename.
many thanks
If you know the depth of the files are only 1 folder in, you can use the following
ECHO OFF
SETLOCAL EnableDelayedExpansion
set include=*.log
FOR /L %%i IN (1,2,3) DO (
net use i: \\my-server%%i\d$\IISLogs
FOR /R i:\ %%G IN (%include%) DO (
FOR /F "tokens=1-2 delims=\" %%H IN ("%%~pnxG") DO (
XCOPY %%G D:\ServerLogsAndBackups\IIS\w%%i\%%H_%%I
)
)
7z a -t7z D:\ServerLogsAndBackups\IIS\w%%i\files%%i.7z *.log -mx9
net use i: /delete
)
If the files are a set number of folders deep, you can adjust the tokens as required and add additional letters to the end of the XCOPY command (i.e. 5 folders deep: tokens=6 and in the XCOPY command it will be %%H_%%I_%%J_%%K_%%L_%%M)
However, if there is a mix of folder depths, you may be better off looking into using something other than Batch scripting to accomplish this.
I need to replace every icon (AutoCAD 2010.LNK) found on the computer with another .LNK using batch.
The icon\ shortcut as we well know can be found anywhere and as many times as the user likes.
How can I achieve this?
first, read HELP FOR
and then try this in a command line
FOR /F "tokens=*" %a in ('dir /B /S "AUTOCAD 2010.LNK"') do ECHO COPY new.lnk %a
experiment with from various locations and test carefully
then create a bat file with the following contents. Note the change of %a into %%a and the removal of the 'echo'
#echo off
PUSHD C:\
FOR /F "tokens=*" %%a in ('dir /B /S "AUTOCAD 2010.LNK"') do COPY new.lnk %%a
POPD
#ECHO OFF
SET "linklist=%USERPROFILE%\linklist.txt"
SET "replacement=D:\path\to\replacement.lnk"
ECHO Searching...
DIR /B /S "C:\AutoCAD 2010.LNK" >%linklist%
DIR /B /S "D:\AutoCAD 2010.LNK" >>%linklist%
:: add similar rows for every drive letter you want to be included
ECHO Replacing...
FOR /F "tokens=*" %%f IN (%linklist%) DO COPY %replacement% %%f
ECHO Finished.
A couple of notes:
Your replacement shortcut file must be named differently (like AutoCAD 2010.LNK.new, for example).
In Windows Vista/7 you will probably be prohibited from overwriting files in certain folders unless you are running the script with elevated rights.