Print request upon new file in folder - windows

I've got the following problem:
I need to make something that checks to see whether a file has been added to a specific folder, ifso this file needs to be printed. I heard Windows maybe has something similar built in?
*Program constantly checks whether a file has been added*
File has been added
File gets printed immediately
I have found solutions, but you need to pay for them.
UPDATE
"Code supplied by Vik"
:start
set SECONDS=60
SET FILENAME=*.jpg
IF EXIST %FILENAME% MSPAINT /p %FILENAME%
choice /C a /T %SECONDS% /D a
DEL /Q %FILENAME%
goto :start
"Edits: COPY *.JPG file to a different folder (E.G. ImageHistory)"
"Edits: DELETE local *.JPG file leaving the monitor folder empty"
Any tips or help are welcome!

This batch file will check if the file printme.jpg exists every 60 seconds. If it exists, it will use the built-in MSPAINT program to print it. Feel free to configure SECONDS and FILENAME to suit your environment.
:start
set SECONDS=60
SET FILENAME=printme.jpg
IF EXIST %FILENAME% MSPAINT /p %FILENAME%
choice /C a /T %SECONDS% /D a
goto :start
Additional mods you may want to make:
If you are using an older version of Windows like XP, you may not have the CHOICE command. In that case, use ping to simulate sleeping: PING 1.1.1.1 -n 1 -w 60000 >NUL
You can add a line to delete the file after it's printed: DEL /Q %FILENAME%
EDIT (Below): Added multi-file, move and delete capability
set SECONDS=20
set FILEFOLDER=C:\dropfolder
set TEMPFOLDER=%FILEFOLDER%\TEMPFOLDER
set FILEWILDCARD=*.jpg
if not exist "%FILEFOLDER%" ECHO %FILEFOLDER% NOT FOUND ... CTRL-C TO EXIT && PAUSE
if not exist "%TEMPFOLDER%" ECHO %TEMPFOLDER% NOT FOUND ... CTRL-C TO EXIT && PAUSE
:start
cd "%FILEFOLDER%"
dir /b "%FILEWILDCARD%" > filelist.txt
for %%A in (filelist.txt) do if not %%~zA==0 goto printfiles
choice /C a /T %SECONDS% /D a
goto :start
:printfiles
echo FILE(s) FOUND!
del /q "%TEMPFOLDER%\%FILEWILDCARD%"
move "%FILEWILDCARD%" "%TEMPFOLDER%"
cd "%TEMPFOLDER%"
for %%A in ("%FILEWILDCARD%") do MSPAINT /p "%%A"
goto :start

Run a VB.Net in Background and use a FileSystemWatcher to get events for each change in that folder. Upon receiving an event, check the file / action and print the file using whatever App that can print them. A Batch file will likely not work here.

Related

Recursively change file extensions to lower case

I have a game that I play and mod a lot, and a lot of the files in the game have file extensions that are in all caps, which bothers me quite a bit. I'm trying to change them all to be lowercase, but there are numerous folders in the game files, so I'm having to be very repetitive. Right now, I'm working with this:
cd\program files (x86)\Activision\X-Men Legends 2\Actors
start ren *.IGB *.igb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\
start ren *.XMLB *.xmlb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\act0\tutorial\tutorial1
start ren *.XMLB *.xmlb
and so on for each and every folder in the game files. I have a very long .bat file where I just have line after line of this but with a different destination folder. Is there a way to streamline this process so I don't have to manually type out each folder name? Also, is there a line that I could add at the beginning to automatically run as an administrator, so I don't have to make sure to run the .bat file as an administrator each time?
I'm not looking for anything complicated, and I'm very inexperienced with coding other than the small amount of stuff I've been able to search up.
Instead of doing it for each folder, use a for /R loop which loops through all subfolders. I would suggest the following code:
#echo off
:prompt
set /p "extensions=What are the up-case extensions you want to convert to lower-case?: "
if not defined extensions (cls & goto:prompt) else (goto:loop)
:loop
for %%A IN (%extensions%) do (
for /R "custom_folder" %%B IN (*.%%A) do (
ren "%%~fB" "%%~nB.%%A"
)
)
Take a look on this on how to run this batch file as admin. Create another batch file and add the code specified in the accepted answer.
Note: As Stephan pointed out in the comments, you can use %ProgramFiles(x86)% environment variable which is the same thing.
#echo off
setlocal
rem Check if admin.
2>nul >nul net session || goto :runasadmin
rem Start in script directory.
pushd "%~dp0" || (
>&2 echo Failed to change directory to "%~dp0".
pause
exit /b 1
)
rem Ask for directory to change to, else use the script directory if undefined.
set "dirpath=%~dp0"
set /p "dirpath=Dir path: "
rem Expand any environmental variables used in input.
call set "dirpath=%dirpath%"
rem Start in the input directory.
pushd "%dirpath%" || (
>&2 echo Failed to change directory to "%dirpath%".
pause
exit /b 1
)
rem Ask for file extensions.
echo File extensions to convert to lowercase, input lowercase.
echo i.e. doc txt
set "fileext="
set /p "fileext=File extension(s): "
if not defined fileext (
>&2 echo Failed to input file extension.
pause
exit /b 1
)
rem Display current settings.
echo dirpath: %dirpath%
echo fileext: %fileext%
pause
rem Do recursive renaming.
for %%A in (%fileext%) do for /r %%B in (*.%%A) do ren "%%~B" "%%~nB.%%A"
rem Restore to previous working directory.
popd
echo Task done.
pause
exit /b 0
:runasadmin
rem Make temporary random directory.
set "tmpdir=%temp%\%random%"
mkdir "%tmpdir%" || (
>&2 echo Failed to create temporary directory.
exit /b 1
)
rem Make VBS file to run cmd.exe as admin.
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "cmd.exe", "/c ""%~f0""", "", "runas", 1
) > "%tmpdir%\getadmin.vbs"
"%tmpdir%\getadmin.vbs"
rem Remove temporary random directory.
rd /s /q "%tmpdir%"
exit /b
This script is expected to start from double-click.
It will restart the script as admin if not already admin.
It will prompt to get information such as directory to change to and get file extensions i.e. doc txt (not *.doc *.txt). If you enter i.e. %cd% as the directory input, it will be expanded.

Copy a file with batch in a user selected directory

i'm looking for a .bat file that, on opening, ask the user what folder he want to select, then, the batch copy a file (python script) in this folder and execute it
for now i use :
xcopy c:/pythonfiletocopy d:/destinationpath
but i dont find a way to make the user choose the destination folder
any idea ?
thank you
You can request user input using the Set command together with its /P option.
Here's an example which should accept typed or pasted input and should also accept drag and drop for directories not containing spaces:
#Echo Off
Set "_in=" & Set /P "_in=Please provide a directory for the file: "
If Defined _in If Exist "%_in%\" Echo XCopy "C:\pythonfiletocopy" "%_in%"
Pause
I have added an Echo on line 3, if you're happy with the output you can remove that.
I know you said you didn't need one but you could JScript launch a GUI directory browser for input.
Here's an example which may do that:
0</* :
#Echo Off
For /F "Delims=" %%A In ('CScript //E:JScript //NoLogo "%~f0" 2^>Nul'
) Do Echo XCopy "C:\pythonfiletocopy" "%%A"
Pause
Exit /B */0;
var Folder=new ActiveXObject('Shell.Application').BrowseForFolder(0,'',1,'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}');
try{new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Folder.Self.Path)};catch(e){};close();
I have added an Echo on line 4, if you're happy with the output you can remove it and line 5.

Get the last name of a directory path in batch

I've been tasked with writing installation scripts for my company that will be able to install printer drivers on a users computer.
What I have done so far is I've made the script accept a parameter (the printer type brother, xerox, etc..) from there it will gather the relative path to the drivers, and run a forfiles command on the path outputting all the drivers, after that it will ask the user for the correct driver copy it from the network drive to the users desktop, and run it.
What I need to do is the running part, I need to copy the driver to the desktop and somehow get the last directory part (example: user\desktop\test.exe I need to gather the last part test.exe) how can I go about doing this in a functional way?
Basically I would need to split the path by \ and grab the last entry of that list, is this possible in batch?
#echo off
type banner.txt
if [%1]==[] goto usage
:verify_argv
IF '%1'=='canon' GOTO get_canon_path
IF '%1'=='xerox' GOTO get_xerox_path
IF '%1'=='hp' GOTO get_hp_path
IF '%1'=='dell' GOTO get_dell_path
IF '%1'=='brother' GOTO get_brother_path
goto :eof
:get_canon_path
set dir_path=\\mgtutils01\windows7apps\PRINTERS\Canon
goto :install_drivers
:get_xerox_path
set dir_path=\\mgtutils01\windows7apps\PRINTERS\Xerox
goto :install_drivers
:get_hp_path
set dir_path=\\mgtutils01\windows7apps\PRINTERS\HP
goto :install_drivers
:get_dell_path
set dir_path=\\mgtutils01\windows7apps\PRINTERS\Dell
goto :install_drivers
:get_brother_path
set dir_path=\\mgtutils01\windows7apps\Brother\Drivers
goto :install_drivers
:usage
#echo Usage: .\driver [PRINTER_TYPE]
exit /B 1
:install_drivers
#echo Finding drivers...
pushd "%dir_path%"
forfiles /s /m *.exe /c "cmd /c echo #relpath"
set /p to_install="Copy the path of the correct driver and paste here: "
#echo Copying file to %USERPROFILE%, please wait..
xcopy %to_install% "%USERPROFILE%\Desktop"
#echo Installing driver..
pushd "%USERPROFILE%\Desktop"
To get the last element of a file or directory path, you can:
either use a for loop and its ~ modifiers:
set "ITEM=user\desktop\test.exe"
for %%I in ("%ITEM%") do set "NAME=%%~nxI"
echo %NAME%
or call a sub-routine by the call command, pass the path as an argument and again use ~ modifiers:
set "ITEM=user\desktop\test.exe"
call :SUB "%ITEM%"
goto :EOF
:SUB
set "NAME=%~nx1"
goto :EOF
For both variants, the ~nx part extracts the base name (n) and the extension (x) from the last element of the path stored in the reference (%%I or %1). Type for /? and call /? into a command prompt window and read the help texts; you will find all the possible ~ modifiers of for variable references and argument references, respectively.

How to get attributes of a file using batch file

I am trying to make a batch file to delete malicious files from pendrive. I know that these malicious files uses hidden,read only and system attributes mainly to hide itself from users. Currently i am deleting these files using cmd by removing malicious files attributes then deleting it. Now I am thinking to make a small batch file which can be used to remove these files just by entering the drive letter.
I have found this code in a website to find attributes of a file. But after entering the name of the file the batch file just exits without showing any results.
#echo off
setlocal enabledelayedexpansion
color 0a
title Find Attributes in Files
:start
set /p atname=Name of the file:
if not exist %atname% (
cls
echo No file of that name exists!
echo.
echo Press any key to go back
pause>nul
goto start
)
for /f %%i in (%atname%) do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
echo Press any key to go back
pause>nul
goto start
can you tell me why this batch file is exiting without showing any results. Or can you give any better batch script for getting attributes of a file.
EDIT
I was able to work the above code only for a single file. As my purpose of my batch file is to remove malicious files by entering the drive letter. How can i use it to find what kind of attributes files are using in a particular drive.
For example:
In cmd we can use this command to find the file attributes of a given drive
attrib *.*
Advance thanks for your help
I tried the bat file (without inspecting the details) and it seems to work fine for me. What I noticed is that it closes instantly if you don't enclose file path with quotation marks - e.g. "file". Example:
Name of the file: path\file.txt // this will close immediately
Name of the file: "path\file.txt" // now it will stay open and display the result
This hopefully solves your problem.
As far as your question in EDIT is concerned, a simple option is to iterate a list of files and execute the batch on each one.
batch1.bat: (%1 refers to the first command-line parameter)
#echo off
setlocal enabledelayedexpansion
echo %1
set atname=%1
for %%i in ("%atname%") do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
Next, generate a list of all files within a given path (say 'folder' including all subfolders):
dir /s /b folder > ListOfFiles.txt
main.bat (read ListOfFiles.txt line-by-line and pass each line to batch1.bat as a command line parameter):
#echo off
for /f "tokens=*" %%l in (ListOfFiles.txt) do (batch1.bat %%l)
Then, from cmd:
main.bat >> output.txt
The last step generates an output file with complete results. Granted, this can be done in a more polished (and probably shorter) way, but that's one obvious direction you could take.
You're using a for /f loop here, which isn't necessary (and may yield undesired results if the filename contains spaces). Change this:
for /f %%i in (%atname%) do set attribs=%%~ai
into this:
for %%i in ("%atname%") do set attribs=%%~ai
This is dangerous code - but it'll delete read only, hidden and system files.
It should fail to run on c: drive but I haven't tested it. Note that some Windows installs are on drives other than c:
#echo off
echo "%cd%"|find /i "c:\" >nul || (
del *.??? /ar /s /f
del *.??? /ah /s
del *.??? /as /s
)

Removing a "sample.ext.ext2" file that has no "sample.ext" file associated with it

The application creates .mta files (with exactly same name) of all multimedia files in my HDD. What I want to do is to check all sub-folders of root folder if there is no multimedia file associated with some .mta then delete it.
Detailed example. Lets say we have files
01.mp3
01.MP3.mta
02.mkv
02.MKV.mta
03.jpg
03.JPG.mta
04.MP4.mta <<==
As you see the last .mta has no original file. I want to delete last file.
I don't know if it's possible with cmd. But following function doesn't work. Please take a look
For /r %%i in (*.mta) do call :nomta %%i
pause
goto end
:nomta
set stem=%1:.mta=%
set original=%stem%.mta
if not exist %original% do exit /B
if not exist %stem% do del /a /Q %1
goto :EOF
:end
echo done
PAUSE
You can use a for command to do this with dir /a feeding it both hidden and non-hidden filenames. Here's an example:
C:\temp\z\z>attrib *
A C:\temp\z\z\foo.bar.mta
A H C:\temp\z\z\h2.mp4.mta
A C:\temp\z\z\hid.mp4
A H C:\temp\z\z\hid.mp4.mta
A C:\temp\z\z\zoo.bar
A C:\temp\z\z\zoo.bar.mta
C:\temp\z\z>for /f %F in ('dir /b/a *.mta') do if not exist "%~nF" echo %F >> z
C:\temp\z\z>type z
foo.bar.mta
h2.mp4.mta
so replacing the echo with a del should achieve your target.

Resources