batch-file - creating a file with file extension - batch-file

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!

Related

Batch script to Read the filenames in a directory and ask user to select one

Thanks for looking into my concern.
I have five config files in a given directory. In my batch script, I want to read those file names and prompt them to user. Once user has selected a config file, read the variables from it.
Could anyone help me with some logic here. So that, I can take it forward.
Thank you.
A batch or .cmd file like this demonstrates the menu technique (nothing fancy, the user has to enter the filename precisely). Key items:
FOR
SET /P
IF EXIST
Good luck!
#echo off
REM Show the user the list and ask them which one to use
echo.
echo Please select one of:
echo.
for %%F in ("D:\A Given Directory\*.config") do echo %%~nxF
echo.
set SEL_CFGFNM=
set /P SEL_CFGFNM=Which configuration file:
REM Make sure they answered, and that the file exists
if "%SEL_CFGFNM%" == "" goto ENDIT
if NOT EXIST "D:\A Given Directory\%SEL_CFGFNM%" goto NOCFG
REM User has selected file "D:\A Given Directory\%SEL_CFGFNM%" and it exists
REM Do whatever you want to do with that file now
REM Don't fall through the exit messages
goto ENDIT
REM Exit Messages
:NOCFG
echo.
echo ERROR: Configuration file "%SEL_CFGFNM%" is not on the list
echo.
goto ENDIT
REM Cleanup
:ENDIT
set SEL_CFGFNM=

Move a single file by extension with a batch file

this is my very first post on this site but as I already found much help on stackoverflow, I now ask this question myself. I did not find an answer to this issue. My English isn't that good and as my Problem is quite complex, I hope you guys are all able to understand me. If not or if there are any questions that are needed to be able to help me, feel free to ask.
So I want to make a Batch file that moves files by the Extension (in my case, it's .pdf) but not all of them. I know there is this command:
move "Y:\Scan\*.pdf" "Y:\Scan\Archive\"
But this will move all pdf files to the Archive Folder and I don't want that.
To get a better overview what my plan is, a small description is needed I guess.
Imagine you have a home Server and your Computer is connected to it, a Printer and a scanner aswell. I have many documents in Folders in my home Office and I want to have them all on my Server. This is done trough scanning all Sheets of paper in These Folders. Everytime I scan something, this is saved as .pdf in a Folder on my local Computer, sorted by time and Looks like this: 2017-06-15-10-30.pdf
My current script is asking 3 times for userinput. One time for a village Name, one time for a street's Name and one time for the number of the house because the Folders for the Archive should be named like the corresponding house they are referring to. So I have These 3 Inputs in my Batch already and also the whole Folder Name saved in a new variable.
My plan is to now ask the user how many pdf's are corresponding to the last created Folder and whatever the user is typing, this amount of files will be stored in that Folder. To understand this a Little better, here is what i've done so far (my Batch code).
#echo off
cls
title Archive-Script (by Relentless)
color 0b
:reset
cls
REM This is the village's name
SET ON=""
REM This is the street's name
SET SN=""
REM This is the number of the house
SET HN=""
REM This is a variable to store whether the user wants to reset the variables or not (used later)
SET RESET=""
:start
cls
IF %RESET% == N echo To overwrite the village's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the village's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %ON%
set /p "ON="
echo The village's name was set to %ON%!
echo.
IF %RESET% == N echo To overwrite the street's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the street's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %SN%
set /p "SN="
echo The street's name was set to %SN%!
echo.
IF %RESET% == N echo To overwrite the house's number, please enter the number again and hit enter. If you don't want to overwrite the number, just hit enter!
IF NOT %RESET% == N echo Please enter the house's number (confirm by pressing enter):
IF %RESET% == N echo Current number: %HN%
set /p "HN="
echo The house's number was set to %HN%!
echo.
echo.
echo.
echo.
echo.
SET FOLDERNAME="%ON%, %SN% %HN%"
echo The folder %FOLDERNAME% will now be created!
mkdir ""%FOLDERNAME%""
timeout /t 2 /nobreak
:pdfmove
cls
echo How many pdf-files are corresponding to the latest created folder?
set /p "PDF="
for /l %%x in (1,1,%PDF%) do (
echo %%x
move Y:\Scan\*.pdf Y:\Scan\%FOLDERNAME%\
)
:resetoption
cls
echo %ON%
echo %SN%
echo %HN%
echo.
echo Should the variables be resetted again? (J/N)
set /p "RESET="
IF %RESET% == J goto reset
IF %RESET% == N goto start
As you can see, I already tried to implement the move Option, but this will move ALL pdf files instead of one on each for Loop. Can anyone help me with that? I guess the code is pretty simple to understand.
Greetz Relentless
for /l %%x in (1,1,%PDF%) do (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
move "Y:\Scan\%%a" Y:\Scan\%FOLDERNAME%\
)
)
I believe what you want to do is to move a user-input number of pdfs.
The above change will, for each %%x, first set the flag moved to nothing, then perform a directory-scan, in /b basic form (filename only) /a-d with no directorynames. Each of the filenames present in the directory will be assigned to %%a in turn. The delims= option allows filenames that contain spaces to be processed.
When the first file is found, its name is echoed with the sequence number and the flag is set to Y (any non-empty string will do). The file is then moved.
When the for...%%a... processes the next filename, moved is now set to a value, so no more file-moves are performed.
The entire process is then repeated for each value of %%x, so %PDF% moves of the first .pdf filename found in the source directory will be moved because one is moved each time and the dir command will not find the file that was last moved because well - it's been moved and hence is no longer in the source directory.
OK, here's a fix to produce a variable number of spaces
SET "tspaces=T"
:addspace
SET "tspaces=%tspaces% "
CALL SET "testspace=%%tspaces:~%pdf%%%"
IF "%testspace%" neq " " GOTO addspace
FOR /L %%x IN (1,1,%pdf%) DO (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
call move "Y:\Scan\%%a" "Y:\Scan\%FOLDERNAME%\olddocumen%%tspaces:~0,%%x%%.pdf"
)
)
The first part sets up tspaces to t+a sufficient number of spaces.
Note that in the second part, the move is now called which resolves the variable-substring. Since %%x varies from 1 to %pdf% then the selection from tspaces is %%x characters - it has a minimum of 1, when the final character of olddocument is required; thereafter a number of spaces follow, so the t is the initial value of tspaces.

autorun script for batch transfer

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

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.

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