Automate group of tasks in one hotkey (delete files, create folder)

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
conejofe
Junior Member
Junior Member
Posts: 5
Joined: 2022-05-02, 03:35 UTC

Automate group of tasks in one hotkey (delete files, create folder)

Post by *conejofe »

Hello :), I would like to know if the following tasks can be automated using a script or macro. Assuming we have a group of files:
File 1.txt
File 2.txt
File 3.txt
File 4.txt
TASKS
1 select all
2 create folder with the name of the last file (File 4)
3 delete the files
So that at the end there is only the folder with the name of the last file (File 4).
I am currently doing this with the hotkey in this order *, F7, ENTER, DELETE, ENTER.
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *Stefan2 »

Hi 2conejofe and welcome.

If you have no other files and folders in that folder with "File x.txt",

then you may try to use such batch:

- get last name (but the dir command may sort in other way than you see in the panel, especially with underscore or such, try it out)
- create directory with last found name
- delete all *.txt files in that folder


do.cmd

Code: Select all

@ECHO OFF
ECHO Working in
ECHO %CD%
ECHO.
PAUSE
for /f "delims=*" %%a in ('dir /b *.txt') DO @SET myFilename=%%~na 
echo "%myFilename%"
MD "%myFilename%"
DEL *.txt
pause

That batch will run in current folder / working directory.
You can create also an button to call that batch just in current visible folder.




 
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *Dalai »

2Stefan2
This is quite dangerous!The dir command doesn't sort anything by default, and it mixes files and folders together. To get around this, I suggest to limit the output to files and to sort by name, although the sort order might still be different from the sort order in TC. My suggestion:

Code: Select all

dir /b /ogn /a-d *.txt
The switch /ogn can be shortened to /on since directories are excluded from being shown anyway. To properly work on UNC paths, the code needs some changes, i.e. the script needs a parameter to the directory in question. It could work like this:

Code: Select all

@ECHO OFF

if "%~1"=="" goto :EOF

pushd "%~1"

ECHO Working in
ECHO %CD%
ECHO.
PAUSE
for /f "delims=*" %%a in ('dir /b /ogn /a-d *.txt') DO @SET myFilename=%%~na 
echo "%myFilename%"
MD "%myFilename%"
DEL *.txt
popd
pause
If parameters are used anyway, why not give a second one with the directory to be created?

Code: Select all

@ECHO OFF

if "%~1"=="" goto :EOF
if "%~2"=="" goto :EOF

pushd "%~1"

ECHO Working in
ECHO %CD%
ECHO.
PAUSE
echo "%~2"
MD "%~2"
DEL *.txt
popd
pause
Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
conejofe
Junior Member
Junior Member
Posts: 5
Joined: 2022-05-02, 03:35 UTC

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *conejofe »

Thank you very much Stefan2 and Dalai for your help. Does that code go in the usercmd.ini file?
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *Dalai »

2conejofe
No, that's a batch file, just as Stefan2 (indirectly) said by writing its file name "do.cmd" above the code block. Save the code as such file - the name is up to you, but the extension must be .cmd or .bat. Then drag the newly saved batch file to a free space of the button bar to create a button. Then edit the button to your needs, and according to the code block you copied:
  • Stefan2's code doesn't need any parameters, but the script works in the current directory, so you need to remove the Start path from the button to allow TC to run the script in the active panel's directory
  • my first full code block needs one parameter: the directory to work in. I.e. add "%P" to the button's parameters.
  • my second full code block needs two parameters: the working directory and the name of the subdirectory to create in the working directory. I.e. add "%P" "%O" to the button's parameters. Note that you need to put the cursor on "file 4.txt" (from your example) before pressing the button in this case.
Tip: Add a question mark at the beginning of the Parameters field to make TC show a dialog before it executes the button's command. This allows to check if the parameters are correct and also allow to make changes if necessary.

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
conejofe
Junior Member
Junior Member
Posts: 5
Joined: 2022-05-02, 03:35 UTC

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *conejofe »

Hi Stefan2 and Dalai, thanks for helping me last time. I still use the code, but would like your help to update it with new features.
The new group contains files and folders interchangeably as the example shows:

System Volume Information (this is a folder)
01-File.mpg
02-File.vob
03-File.mkv
04-File.ts
05-File.avi
06-File.mp4
07-File.mpg
ZentimoSettings.ini

Task:
1. Create a folder named after the last video file listed, regardless of its extension (07-File).
2. Delete all files and folders, only the folder (07-File) remains.
Also, I need the code to be as unattended as possible, when I run it a CMD window pops up and forces me to press a key twice. Can this be removed?
Regards
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *Dalai »

2conejofe
That is quite a different task.

My suggestion:

Code: Select all

@echo off

if "%~1"=="" goto :EOF

setlocal

set videoextensions=mp4 mpg avi mkv ts vob mov

pushd "%~1" || goto END

echo.
echo Working in %CD%
echo.
echo Video file extensions: %videoextensions%
echo.
pause

for /F "delims=" %%F IN ('dir /b /o-n /a-d *.*') DO (
    for %%V IN (%videoextensions%) DO (
        if /i "%%~xF"==".%%V" (
            echo Found extension "%%~xF"
            echo Creating "%%~nF"
            REM *
            echo mkdir "%%~nF"
            echo del /F /Q *.*
            REM *
            goto END
        )
    )
)
echo Nothing found

:END
endlocal
popd
echo.
pause
The script expects one parameter: the directory to work in. You can create a button as before and add %P in the button's parameter field, for example.

Important things about the code above:
  • Since video file types are quite extensive, you can add file extensions to the variable "videoextensions" or remove them from it as needed. But keep everything else in there, especially the spaces (they're required for the for-loop to work).
  • The script DOES NOT remove any directories. Such feature can be added, but since it's potentially dangerous to delete entire directory structures - especially since the deletion via the rmdir and del commands is permanent, meaning there's no recycle bin involved - I left it out for now.
  • As is, the code does nothing but output what it would do. Run the script and if you think you're happy with the result, you can proceed with the next step below.
  • To make the code actually do something, remove the echos in front of the mkdir and del commands marked between the two REMs with the asterisk after it.
  • The script can be modified further to make it run in an unattended manner. Remove the "pause" commands, or comment them out by prefixing them with a REM (or a double colon).
  • Keep in mind that the dir command might still sort files different than TC, especially when you have enabled natural sorting in TC. The sort order plays a major role in finding "the last video file", as you call it.
Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
conejofe
Junior Member
Junior Member
Posts: 5
Joined: 2022-05-02, 03:35 UTC

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *conejofe »

hello Dalai, the code does not work on files saved in folders with spaces in their name.Any solution that is not to replace the spaces by underscore in the name of the folders?
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: Automate group of tasks in one hotkey (delete files, create folder)

Post by *Dalai »

Works fine here with spaces in file and/or directory names. You have put quotes around the script's parameter, haven't you?

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
Post Reply