autorun script for batch transfer - batch-file

I am not a programmer in the least. I read through a few posts before asking to see if any would apply to my seemingly simple question. Unfortunately I did not find any that explained what I am looking for.
My question is:
I want to be able to send a USB stick to my Mom every month filled with pictures. She is not computer savvy in the least.
I would like for the process to be simple and duplicate-able for other members of my family also.
On my Mom's computer I made a directory C:\Family Pictures. That is where all the pictures are that she can open easily to see them.
Each month I'd like to send her more pictures(Lot's of them). I would like to put them all on a USB and when she plugs it in they will automatically transfer from usb to designated folder on drive C mentioned above.
We both use Windows 10 and it is my understanding that my 6 brothers & Sisters are as well. This way all of my family can send her USBs every month.
Is this even possible? I would love to learn how to create this there by making it simple for Mom :)
Thanks in Advance,
Me

Autorun is no longer available since Windows 7, so you can place a batch file in the desktop and tell her to insert the USB drive and then run the desktop script in sequence.
The code would be something like this:
#echo off
setlocal
:=============================================================
:OPTIONS - you can change these values
set _SourcePath=C:\Family Pictures
set _DestPath=X:\Family Pictures\Mama
:=============================================================
set _LogPath=%_DestPath%\_log.txt
echo/
echo/This will copy the family files:
echo/ FROM: %_SourcePath%
echo/ TO: %_DestPath%
echo/
:prompt1
set /p msgbox=Continue? [Y/N]
if /i "%msgbox%"=="n" (echo/"Aborted..." &goto:end)
if /i "%msgbox%"=="y" goto:step1
goto:prompt1
:step1
if exist "%_DestPath%\" goto:step2
echo/
echo Folder %_DestPath% does not exist.
echo/
:prompt2
set /p msgbox=Create folder? [Y/N]
if /i "%msgbox%"=="n" (echo/"Aborted..." &goto:end)
if /i "%msgbox%"=="y" goto:CreateFolder
goto:prompt2
:CreateFolder
md "%_DestPath%"
echo %errorlevel%
if not %ERRORLEVEL%==0 (echo/Aborting: error while creating destination folder &goto:end)
:step2
echo/
echo/ Copying files...
echo/
xcopy "%_SourcePath%" "%_DestPath%" /Z /Y /E > "%_LogPath%"
if %ERRORLEVEL%==0 goto:DeleteLog
:prompt3
set /p msgbox=There were errors copying the files. View log file? [Y/N]
if /i "%msgbox%"=="n" goto:DeleteLog
if /i "%msgbox%"=="y" (notepad "%_LogPath%" &goto:end)
goto:prompt3
:DeleteLog
del "%_LogPath%"
:end
endlocal
NOTES:
Change the paths under :OPTIONS to the desired paths

Related

batch-file - creating a file with file extension

I am a student and new here in Stack Overflow.
I am currently creating a project for my Operating System class. One of the menu would be this: The user can create, rename and delete a certain file anywhere in the directory. I already know how to rename and delete a file but I had a hard time finding the right command to create a file wherein the user can determine what file extension that certain file would be.
It is sort of like this:
//this is to show the user what available drives the computer has
echo Here are the available drives:
echo.
wmic logicaldisk get name
echo.
echo.
set /p ch2=Which drive do you want to access?
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do #echo %%x >>drive%%x.txt
if exist D:\drive!ch2! (
goto specificview
)
else (
goto Invalid
)
//here the chosen drive will be viewed with its saved files and the user is asked what to do next
:specificview
cls
set ch3=
dir !ch2!:\ /w /a
pause
echo.
echo.
echo What do you want to do in this directory?
echo ================================================================
echo Press [X] to go back to choose directory
echo Press [w] to Exit
echo Press [C] to create File
echo If none of the choices, type down the name of the directory file
echo NOTE: Just press [1] to go back to the main MENU
echo ================================================================
//let's say the user will choose C to create file
set /p ch3= Please Choose:
if /i !ch3!==c (
set /p crt=Input the name of the filename:
set /p ext=Input the desired extension:
Now, I don't know what command I should use in order to complete the code. I would like to ask your help so that I can finish my project.
Thanks a lot in advance.
Not really comment, not really answer so I will make it CW!
As this question is not really a duplicate I will add a few things to change it to your needs.
I assume that your file is going to be empty and not using some special formatting or encoding.
As already visible in the answers to above question, there are a lot of ways to do that. I would suggest the answer with the most upvotes that copies NUL to a new file making it empty.
The copy command as you can see is quite simply structured: copy firstFile.something secondFile.somethingElse or in this case copy NUL fileName.ext
So in your case you would go ahead and add the following line:
copy NUL !crt!.!ext!

search for partial folder name and return dir to use with batch

Firstly i am still a beginner at scripting so please be gentle.
I am creating a back up script and I want to be able to search network for part of a file name (which located on a networked External HDD for Backup use), then return the dir to use later to back up the contents of a USB device.
The folder name I am searching for consists of 4 elements as follows: Date-1234567-Type-User.
The part i am looking to search for is the 7 digit id number which is unique where as the other elements may be duplicated a number of times.
the current script i have been using is not returning anything it is giving file not found, could someone point me in the right direction my script is as follows, also i am working on windows 7.
#echo off
:start
set /p path=enter id no.:
for /r %%# in (.) do (
Echo %%~nx# | find "string" "%path%" 1>NUL && (
Echo Full Path: %%~nx#
Echo Filename : %%~nx#
Echo Directory: %%~p#
)
)
if defined p (
echo %p%
) else ( Echo file not found
)
pause
goto backup
:backup
if not exist "%p%"\"usb download" md "%p%"\"usb download"
if exist (
xcopy /y "USB" "%p%"\usb download
)
pause
goto start
the Xcopy lines work on their own creating a new folder and copying the contents however i have to write the complete file path manually which is quite extensive and laborious so if i could automate this it would help dramatically.
I thank you for your help in advance, I have googled and searched my heart out, but as I said I am a beginner and my brain is melting.
thanks all.
for /f "delims=" %A in ('dir /ad /b /s z:\*1234567*') do echo %A
This should find it.

Batch file with "menu" for deleting files referencing path from a text file

Good Day to All,
Please pardon me if my question may appear odd. I was not able to pinpoint past examples that suit the ideal script that i wish to accomplish.
Currently, i have the following script. Essentially is to delete files from folders whose location is listed in a text file (del_location.txt).
For /F "tokens=*" %%A in (del_location.txt) do del \Q "%%A"
I did not choose to use Forfiles, as the network i am working on in my company is at Windows 2000 without the ability to run Forfiles and due to security reasons, are not able to install any patches.
There are multiple location in del_location.txt and there are instances that my users may not wish to delete all the listed location. Hence i am wondering how do i write a "menu" function, that reads off the listed location in del_location.txt and ECHO it as a selection. Lets the user choose the selection in a Yes/No fashion before the commencement of the delete sequence? Thus allowing the user to "walk off" from the terminal after choosing what he wants to delete.
*Also, from as far as i can find online, without Forfiles, i would not be able to choose to delete only files that are older than N days. Am i right to state so?
Your help and advices are greatly appreciated.
You can set each lines in del_location.txt to an array. Since batch don't support array, then we need to do it manually.
#echo off
setlocal enabledelayedexpansion
:main
set lines=0
For /F "tokens=*" %%A in (del_location.txt) do (
set array[!lines!]=%%A
set /a lines+=1
echo !lines!^) %%A
)
echo. & echo.
set /p choice=Which file do you want to delete? ^>
if %choice% lss 1 goto main
if %choice% gtr %lines% goto main
set /a choice-=1
del /Q !array[%choice%]!
echo. & echo.
echo "!array[%choice%]!" was deleted successfully ^^!
echo. & echo.
choice /c:yn /m "Do you want to delete more files? [y/n]" /n
if %errorlevel% == 1 goto main
if %errorlevel% == 2 exit /b
This script only provided for delete 1 file per turn. So if you want to batch delete, try to modify it yourself, ask anyone or me if you stuck at a point.
I think there's a way to delete files based on date other than forfiles method. Btw, dir SOURCE /o:d list and sort out files based on date.

Continue batch file from previous point

I am playing a alpha game which when crashes corrupts the saved game, so i created a batch file to back up my saved game.
#echo off
:A
REM deletes old backup
RMDIR /S /Q "backup location 1"
REM creates back up
xcopy "game location" "backup location 1" /e /i /q /r /y
REM adds time out for 300 seconds
timeout /t 300
REM deletes old backup
RMDIR /S /Q "backup location 2"
REM creates back up
xcopy "game location" "backup location 2" /e /i /q /r /y
REM adds time out for 300 seconds
timeout /t 300
REM Repeats
Goto A
This works just fine but it always stars on the first back up.
What I want to do is have it continue from the previous point of backing up.
I created the following test file
#echo off
set point=a
echo The variable is "%point%"
if %point%==c (goto :a)
if %point%==a (goto :b)
if %point%==b (goto :c)
:a
Echo A
set point=a
echo The variable is now "%point%"
pause
:b
Echo B
set point=b
echo The variable is now "%point%"
Pause
:c
Echo C
set point=c
echo The variable is now "%point%"
Pause
When the file is restarted the point variable is the same.
Is it possible to have the set point= save the variable to the file for the next time it is run?
Can you please explain any code suggested, I can use google to research it myself and probably will to get a better understanding. But if it is too complicated I may not know what I am looking at to google it.
Thank you for any help
In some versions of DOS you can use the /p argument on the set command to load contents of a file into the variable.
set /p point=<a.txt
Where a.txt contains the value you want to set the "point" variable to.
best way to "survive" for data is a file.
you can write a variable to a file with:
echo %variable%>filename.txt
and read it back with
set /p variable=<filename.txt
This works fine with a single line (for example one variable). This method can be extended for a few more lines. If you want to save many (or an unknown number of) lines, there are better methods.
Thank you for the replies. I have not looked at the /p but will do for a future script I have planned.
For the requirement of this script thank you indiv. For some reason having them in a time stamped folder eluded (even thou I have used them before).
To have the desired effect with just one file I changed
xcopy "game location" "backup location 1" /e /i /q /r /y
to
xcopy "game location" "backup location %date:/=-% %time::=-%\" /e /i /q /r /y
and had the one command repeat itself.
On a side not if using this method there will be an unlimited number of folders. is there a way to limit folders to a z amount and delete older ones?
I am happy deleting older backups, I am just curious for future projects

I Want To Copy Files with set /p On Batch

I want to copy a file with set /p. Task: Write a file name with set /p and that will be copied in a directory that I want, but it doesn't work.
My current source:
#echo off
echo Enter YOur Pic Name With .jpg
set /p cop=
xcopy /s %systemroot%\%cop% %systemroot%\system32\oobe\info\backgrounds
cls
pause
For the SET /P command, the format works best like this:
set /p cop=Enter your Pic name with .jpg
I'd also suggest you add a couple of lines to check if the file is actually there:
#rem just check that the full path is what I expect
echo %systemroot%\%cop%
#rem and check if the file is there
dir %systemroot%\%cop%
(Delete these lines once your batch file is working).
Also, delete the cls line, until it's working. Then, once it's doing what you need, you can put it back in, if you want.
You might want to think about whether %systemroot% is the right place for these pictures, even temporarily. It's usually used for Windows OS code.
It is possible that %systemroot% path may have spaces, so file names must be enclosed in quotes:
#echo off
set /p "cop=Enter YOur Pic Name With .jpg: "
xcopy /s "%systemroot%\%cop%" "%systemroot%\system32\oobe\info\backgrounds"
cls
pause
try running as admin: http://windows.microsoft.com/en-us/windows7/how-do-i-run-an-application-once-with-a-full-administrator-access-token
also you may try the following (xcopy doesn't like the /s), I think you're trying to silence the output, just try with the >nul redirect:
#echo off
echo Enter YOur Pic Name With .jpg
set /p cop=
xcopy /y %systemroot%\%cop% %systemroot%\system32\oobe\info\backgrounds\* >nul
cls
pause

Resources