Copying files over the network using Batch File - batch-file

#ECHO OFF
set /p TerminalName= Enter the PC you wish to relocate ECFs on:
ECHO Do you wish to relocate the ECFs on %TerminalName% ?
PAUSE
IF NOT EXIST "\\%TerminalName%\c$\Program Files\Google" (
ECHO You don't have Google installed
) ELSE (
ECHO You have Google installed!
ECHO Relocating the ECF Files! Here we go...
IF EXIST "\\%TerminalName%\c$\Program Files\Google" (
ECHO The ECF Folder already exists..
ECHO Moving ECFs now
cd \\%TerminalName%\c$\Program Files\Google
FOR %%f IN (*.ecf) DO move /y "%%f" "\\%TerminalName%\c$\Program Files\Google"
) ELSE (
ECHO No ECF Folder exists...Let's sort that out!
MKDIR "\\%TerminalName%\c$\Program Files\Google"
cd \\%TerminalName%\c$\Program Files\Google
ECHO Moving ECFs now
FOR %%f IN (*.ecf) DO move /y "%%f" "\\%TerminalName%\c$\Program Files\Google"
)
)
ECHO Finished!
PAUSE
Whenever running the above Batch file i get this error:
CMD does not support UNC paths as current directories.
Ps - Sorry for the formatting...it won't all go into the grey for code :(

Here is a batch code which should work for your task:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET /P "TerminalName=Enter the PC you wish to relocate ECFs on: "
ECHO Do you wish to relocate the ECFs on !TerminalName! ?
PAUSE
SET "SourcePath=\\RemoteComputer\\c$\Program Files\Google\ECF_Folder"
SET "TargetPath=\\!TerminalName!\c$\Program Files\Google"
IF NOT EXIST "!TargetPath!" (
ECHO You don't have Google installed.
GOTO EndMoveECF
)
ECHO You have Google installed!
ECHO Relocating the ECF files! Here we go...
IF EXIST "%TargetPath%\ECF_Folder" (
ECHO The ECF folder already exists..
) ELSE (
ECHO No ECF Folder exists... Let's sort that out!
MKDIR "%TargetPath%\ECF_Folder"
IF ERRORLEVEL 1 (
ECHO Failed to create ECF folder "%TargetPath%\ECF_Folder".
GOTO EndMoveECF
)
)
ECHO Moving ECF files now ...
MOVE /Y "%SourcePath%\*" "%TargetPath%\ECF_Folder\"
ECHO Finished!
:EndMoveECF
ENDLOCAL
PAUSE
You need to set SourcePath accordingly. And you need to replace all occurrences of ECF_Folder by whatever is right in your environment.
Moving the files is done with command MOVE without switching current working directory as this is not needed. And command MOVE supports also wildcards and therefore no need for a FOR loop.
Delayed environment variable expansion is used partly in case of user of batch file enters an invalid terminal name containing for example a double quote, angle brackets or other characters with a special meaning in batch files. Open a command prompt window and execute in this window set /? for details about delayed expansion.
There is once !TerminalName! and once !TargetPath! used instead of %TerminalName% and %TargetPath%. After existence of Google directory on remote computer is positive verified, it should be safe to reference TargetPath without delayed expansion.

Instead of CD, the PUSHD command can be used. Remember, that internally PUSHD will do a NET USE and consume a "drive letter" from the OS. If this is done too many times, the system will run out of "drive letters."
Remember to POPD at an appropriate time.
UPDATE: Looking at this again, I doubt if you ever need to change directories. Where do the files exist that are to be MOVE'd to the new directory? More analysis needs to be done.

Related

How to make a Batch File Move another File?

I am trying to make a batch script that moves a file into a startup folder and I want to make it universal
Currently I have this but I want to make it work on any user's computer (C:\users\USERNAME)
Here is the code
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %name%
move C:%user%\Desktop\Directory 1\dile.txt C:%user%\Desktop\Directory 1\file folder 1
:end
cmd /k
The file is called dile.txt located in a folder called Directory 1 on the desktop and I want to make it move to a folder called file folder 1 inside the Directory 1 folder. Is there a way to do this while making it work on anyone's computer?
%USERNAME% can be used to grab the active user account. Try something like this. Make sure to enclose paths in quotes when folders have spaces in their names.
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %USERNAME%
move "C:\Users\%USERNAME%\Desktop\Directory 1\file.txt" "C:\Users\%USERNAME%\Desktop\Directory 1\file folder 1\"
:end
cmd /k
Just for the sake of providing something a little bit different:
#Echo Off & SetLocal EnableExtensions
Set /P "=Startup . " 0< NUL
For /L %%G In (1,1,5) Do (Set /P "=. " 0< NUL
%SystemRoot%\System32\PATHPING.EXE 127.0.0.1 -n -q 1 -p 500 1> NUL)
Echo(&Echo Your name is %UserName%
%SystemRoot%\System32\Robocopy.exe "%UserProfile%\Desktop\Directory 1" "%UserProfile%\Desktop\Directory 1\file folder 1" "dile.txt" /Mov 1> NUL 2>&1
Pause
This uses some animated text, %UserProfile%, instead of C:\Users\%UserName%, and robocopy instead of Move. Using RoboCopy, allows for the creation of your destination directory, if it does not already exist.

How do you use batch IF EXIST to test directories?

The batch here inserts file correctly but provides odd output for the IF EXIST. I have verified the issue as being with the statement by the echos before and after it, but the IF EXIST is pinging as true if the copy is going off. The error I get is the console text of "The system can not find the drive specified."
Code is below.
ECHO OFF
ECHO This batch file will place the background and user icons for Windows 7 install.
SET directoryName=C:\Users\yourname\Desktop\BatchTestingFolder\ImageInsertReal\testfolder
ECHO %directoryName%
PAUSE
IF EXIST guest.bmp (
::If image exists
ECHO 1
::1--
IF EXIST %directoryName% (
::If directory exists
::insert all below images
::2--
ECHO 2
COPY /-Y guest.bmp %directoryName% ) ELSE (
::Else echo directory doesnt exist
::2--
ECHO The folder %directoryName% does not exist.
goto ENDER ) ) ELSE (
::Else echo image doesn't exist
::1--
ECHO Images do not exist in current batch file directory.
goto ENDER )
::Exit insertion
:ENDER
PAUSE
I would highly advise you use a syntax of coding that is readable.
Proper indentation helps with readability of parentheses code blocks.
Using a double colon as a comment inside a parentheses code block can cause undesirable code output.
You can use a backslash to make sure you are testing for the existence of a directory.
Use quotes around your file names and file paths to protect spaces and special characters.
This may fix your problems.
#ECHO OFF
ECHO This batch file will place the background and user icons for Windows 7 install.
SET "directoryName=C:\Users\yourname\Desktop\BatchTestingFolder\ImageInsertReal\testfolder"
ECHO %directoryName%
PAUSE
IF EXIST guest.bmp (
REM If image exists
ECHO 1
REM 1--
IF EXIST "%directoryName%\" (
REM If directory exists
REM insert all below images
REM 2--
ECHO 2
COPY /-Y guest.bmp "%directoryName%\"
) ELSE (
REM Else echo directory doesnt exist
REM 2--
ECHO The folder %directoryName% does not exist.
goto ENDER
)
) ELSE (
REM Else echo image doesn't exist
REM 1--
ECHO Images do not exist in current batch file directory.
goto ENDER
)
::Exit insertion
:ENDER
PAUSE

Batch file set command isn't working in if statement

For the life of me, I can't figure out why the below set prompt won't work when it is in the if statement:
#echo off
REM :askdeletecsvs
if exist *.csv (
echo Warning! All files in the scripts folder that have the "CSV" extension will be deleted!
echo Answering "n" will continue the script without deleting the CSVs.
set /p ASKDELETE=Delete CSVs? (y/n):
REM
REM if ( /i %ASKDELETE% equ "y" goto :deletecsvs )
REM if ( /i %ASKDELETE% equ "n" goto :runscripts )
REM goto :askdeletecsvs
)
When I run the batch file as it is above the cmd window opens and then shuts quickly. If I move the set line outside of the if statement then the prompt shows as expected. (There are csvs in the folder the bat file is running from)
What am I missing?
To start with you had used a closing parenthesis which was prematurely ending your opening If parenthesis.
I'd suggest reversing the thinking:
If Not Exist *.csv GoTo runscripts
Echo Warning!
Echo All files in the scripts folder that have the "CSV" extension will be deleted!
Echo Answering "N" will continue the script without deleting the CSVs.
Choice /M "Delete CSVs"
If ErrorLevel 2 GoTo runscripts
:deletecsvs
Del /F /Q /A "PathTo\scripts\*.csv"
GoTo :EOF
:runscripts
You can change GoTo :EOF to a relevant valid label as necessary or remove it if you want to continue on to :runscripts. You can also replace PathTo\scripts\ with %~dp0 if the batch file is running from the scripts directory, or remove PathTo\scripts\ if the current directory holds those files. (note that the current directory and batch file path may not necessarily be the same)

uTorrent Batch Script

I wrote myself a script based off another one that I found and I'm having trouble figuring out why it's not working.
How it is supposed to work is once a torrent has finished downloading, it runs the script and grabs the Label on the torrent. For testing, I was downloading a song with the label of Music.
When it gets to the point at :copyfile, it won't move it into the correct directory. Instead of moving into F:\Completed Torrents\Music, it just moves into F:\Completed Torrents.
Can someone please point out what I'm missing because I've looked through it thrice already and it's driving me crazy. The script is below.
#echo off
title Liam's torrent-file script
rem Parameter usage: fromdir torrent-name label kind [filename]
rem corresponds to uTorrents flags: %D %N %L %K %F
echo *********************************************
echo Run on %date% at %time%
set fromdir=%1
set name=%2
set label=%3
set kind=%4
set filename=%5
set savepartition="F:\Completed Torrents"
set winrar="C:\Program Files (x86)\WinRAR\WinRAR.exe"
set torrentlog="F:\Torrent Scripts\logs\torrentlog.txt"
set handledlog="F:\Torrent Scripts\logs\handled_torrents.txt"
set errorlog="F:\Torrent Scripts\logs\ErrorLog.txt"
set label_prefix=""
echo Input: %fromdir% %name% %label% %kind% %filename%
rem Check if the label has a sub label by searching for \
if x%label:\=%==x%label% goto skipsublabel
rem Has a sub label so split into prefix and suffix so we can process properly later
echo sub label
for /f "tokens=1,2 delims=\ " %%a in ("%label%") do set label_prefix=%%a&set label_suffix=%%b
rem add the removed quote mark
set label_prefix=%label_prefix%"
set label_suffix="%label_suffix%
echo.prefix : %label_prefix%
echo.suffix : %label_suffix%
goto:startprocess
:skipsublabel
echo Skipped Sub Label
goto:startprocess
:startprocess
echo %date% at %time%: Handling %label% torrent %name% >> %handledlog%
rem Process the label
if %label%=="Movies" goto known
if %label%=="Music" goto known
if %label_prefix%=="TV" goto TV
rem Last resort
rem Double underscores so the folders are easier to spot (listed on top in explorer)
echo Last Resort
set todir=%savepartition%\Unsorted\__%name%
if %kind%=="single" goto copyfile
if %kind%=="multi" goto copyall
GOTO:EOF
:known
echo **Known Download Type - %label%
set todir=%savepartition%\%label%\%name%
echo todir = %todir%
GOTO:process
:TV
echo **Known Download Type - %label%
set todir=%savepartition%\%label_prefix%\%label_suffix%
echo todir = %todir%
GOTO:process
:process
rem If there are rar files in the folder, extract them.
rem If there are mkvs, copy them. Check for rars first in case there is a sample.mkv, then we want the rars
if %kind%=="single" goto copyfile
if exist %fromdir%\*.rar goto extractrar
if exist %fromdir%\*.mkv goto copymkvs
if %kind%=="multi" goto copyall
echo Guess we didnt find anything
GOTO:EOF
:copyall
echo **Type unidentified so copying all
echo Copy all contents of %fromdir% to %todir%
xcopy %fromdir%\*.* %todir% /S /I /Y
GOTO:EOF
:copyfile
rem Copies single file from fromdir to todir
echo Single file so just copying
echo Copy %filename% from %fromdir% to %todir%
xcopy %fromdir%\%filename% %todir%\ /S /Y
GOTO:EOF
:copymkvs
echo Copy all mkvs from %fromdir% and subdirs to %todir%
xcopy %fromdir%\*.mkv %todir% /S /I /Y
GOTO:EOF
:extractrar
echo Extracts all rars in %fromdir% to %todir%.
rem Requires WinRar installed to c:\Program files
if not exist %todir% mkdir %todir%
IF EXIST %fromdir%\subs xcopy %fromdir%\subs %todir% /S /I /Y
IF EXIST %fromdir%\subtitles xcopy %fromdir%\subtitles %todir% /S /I /Y
call %winrar% x %fromdir%\*.rar *.* %todir% -IBCK -ilog"%todir%\RarErrors.log"
IF EXIST %fromdir%\*.nfo xcopy %fromdir%\*.nfo %todir% /S /I /Y
GOTO:EOF
EDIT
Also, for some reason, on line 39 nothing prints to the log. For those who wish to see the code with line numbers: http://hastebin.com/juqokefoxa.dos
A couple of bits for ya:
1) Likely, your script isn't moving the files. Preferences / Directories has an option to move downloads when completed. verify that these settings aren't doing the file moving.
2) uTorrent locks the files on completion so that seeding can continue. To change this behavior, go to Preferences / Advanced and set bt.read_only_on_complete to false
3) you will still be foiled because "Run this program when a torrent finishes" doesn't really do what it says. It runs the program as downloading reaches 100%, but while uTorrent is still either moving the file or seeding. See my bug report here.
A quick summary of the post, just in case that post gets deleted: you have to set the command in "Run this program when a torrent changes state:", add a %S parameter and check that %S == 11
4) Just a tip from my attempt at doing something very similar: when you set the variables from the arguments, add a tilde (%~1 instead of %1). This will strip the quotes off and let us more easily build command lines with the variables later.
You say that the log is not being written to. Try this as a test and see if it writes to the log.
If it doesn't there there is some other fundamental problem.
#echo off
title Liam's torrent-file script
rem Parameter usage: fromdir torrent-name label kind [filename]
rem corresponds to uTorrents flags: %D %N %L %K %F
echo *********************************************
echo Run on %date% at %time%
set "fromdir=%~1"
set "name=%~2"
set "label=%~3"
set "kind=%~4"
set "filename=%~5"
set "savepartition=F:\Completed Torrents"
set "winrar=C:\Program Files (x86)\WinRAR\WinRAR.exe"
set "torrentlog=F:\Torrent Scripts\logs\torrentlog.txt"
set "handledlog=F:\Torrent Scripts\logs\handled_torrents.txt"
set "errorlog=F:\Torrent Scripts\logs\ErrorLog.txt"
set "label_prefix="
set "handledlog=%userprofile%\desktop\handled_torrents.txt"
>> "%handledlog%" echo Input: "%fromdir%" "%name%" "%label%" "%kind%" "%filename%"
>> "%handledlog%" echo %date% at %time%: Handling "%label%" torrent "%name%"

How can you create a batch script which runs the user-specified software?

So basically I want to create a batch script that can run any notepad file which the user specifies. I tried this...
#Echo Off
SET /P ANSWER=What is the name of the file to open?
IF /i (%ANSWER%)==('FIND /i "*.txt" %ANSWER%) (goto :Filename)
goto :exit
:Filename
Start *.txt
EXIT
:exit
ECHO FAILLLLLLLL
PAUSE
EXIT
The issue here is the first IF statement. I know its wrong. But, I don't know how to specify the entry of any filename. A different way to do this task is also appreciated.
Thanks for help :)
If your goal is simply to open a file that the user specifies in Notepad, the following works for me in Windows 7:
#echo off
set /P answer=What is the file name?
if exist %answer% (
start notepad.exe %answer%
) else (
echo Unable to locate %answer%
)

Resources