Menu
  • HOME
  • TAGS

bash: rename files in indeterminate sub-subfolders based on grandparent directory name

osx,bash,directory,batch-rename

The following script will work as desired : dir=$1 find $dir -name "*Screenshot*" -type f | while read file do base=$(basename $file) dirpath=$(dirname $file) extr=$(echo $file | awk -F/ '{print $(NF-2)}') #extracts the grandparent directory mv $file $dirpath/$extr-$base done As @loneswap mentioned, this must be invoked as a script. So...

Use of regular expression to exclude characters in file rename,Python?

python,regex,python-2.7,batch-rename

re.sub() returns the altered string, but you ignore the return value. You want to re-assign the result to filename_zero: filename_zero = re.sub("[^\d-]", "", filename_zero) I've corrected your regular expression as well; this removes anything that is not a digit or a dash from the base filename: >>> re.sub(r'[^\d-]', '', '1234567-1(crop...

Parse and Switch Elements of Folder Names with Powershell 2.0

powershell,powershell-v2.0,reverse,batch-rename

This should get the job done. $Path = "C:\Users\Test" $regex = "^(Spring|Summer|Fall|Winter)\s+\d{4}$" Get-ChildItem $Path | Where-Object {$_.PSIsContainer -and $_.Name -match $regex} | Rename-Item -NewName {$split = $_.Name -split "\s+"; "{0} {1}" -f $split[1],$split[0]} We use that regex to filter out the folder that fit your convention. Should be a little...

cmd for loop mass renaming again oneliner

for-loop,cmd,batch-rename,variable-expansion

for %a in (*) do ( set tmpx=%a & echo %tmpx% ) The problem with the previous code is delayed expansion. Yes, you enabled it, but you have not used it, and depending on how you enabled it, it will not work In cmd, when a line or block of...

Add Path to File Name in Batch Delete File

batch-file,batch-processing,batch-rename

In the simplest case: as your text file lines have relative paths then the batch file has to be executed from the relative folder in question. If your text file has fully qualified paths names then it can be launched from anywhere....

Bat script to rename files

batch-file,batch-rename

Try, in place of for %%j in (*.jpg) do ( for /f "delims=" %%j in ('dir /b /a-d *.jpg') do ( if you ECHO REN... instead of REN, just temporarily, then batch will simply report the required rename, but not execute it. Remove the ECHO to perform the rename What...

Files having same prefix to be moved to another directory

windows,batch-file,cmd,batch-rename

Not tested: @echo off setlocal set "source_dir=C:\source" set "destination_dir=C:\dest" set /p "mask=Enter a pattern: " pushd "%source_dir%" for %%# in ("%mask%_*") do ( copy /y "%%~#" "%destination_dir%" ) endlocal ...

windows batch rename files with padded numeric file?

windows,batch-file,batch-rename

@echo off for /L %%i in (1,1,9) do ren "%1%%i.mp3" "%10%%i.mp3" Previous Batch file requires the initial name in the first parameter, for example: test.bat disc5 You may also use previous method directly in the command-line: for /L %i in (1,1,9) do ren "disc5%i.mp3" "disc50%i.mp3" ...

Trying to rename .JPG to .jpg in shell CLI [closed]

linux,shell,rename,command-line-interface,batch-rename

If you don't want to use rename, you mention you have tried various things, then with only built-in bash utils, you can do this. for x in `find . -maxdepth 1 -type f -name "*.JPG"` ; do mv "$x" `echo $x|sed 's/JPG/jpg/g'`; done The backticks around find run the expression...

Change filenames in bulk

batch-file,batch-rename

@ECHO OFF SETLOCAL SET "sourcedir=U:\sourcedir" FOR /f "tokens=1,3delims=)-." %%a IN ( 'dir /b /a-d "%sourcedir%\*) - *.kam" ' ) DO FOR /f "tokens=*" %%u IN ("%%b") DO ECHO REN "%sourcedir%\%%a) -%%b.kam" "%%u - %%a).kam" GOTO :EOF This should solve your problem. You'd need to change yur sourcedir of course. The...

C# Reading through CSV file to rename multiple files with new names

c#,csv,file-rename,batch-rename

In my opinion, a better solution would be to use a plain old foreach and not a call to ToList().ForEach(). var lines = File.ReadAllLines(filepath); var data = from l in lines.Skip(1) let split = l.Split(',') select new OldNew { oldFile = split[0], newFile = split[1], }; foreach(var f in data)...

Batch file rename reading old file name and new file name from a txt file

batch-file,file-rename,batch-rename

This should do what you have described. Remove the echo after the do when you have tested it - atm it will merely print the commands to the screen. @echo off for /f "usebackq tokens=1,*" %%a in ("file.txt") do echo ren "%%a" "%%b" pause ...

Batch editing files 'stuck at weird place'

bash,shell,scripting,batch-rename

If you want to move the file, you have to use the path, too, otherwise mv wouldn't be able to find it. The target specification for the mv command is more problematic, though. You're using "$fhello.dat" which, in fact, means "content of the $fhello variable plus the string .dat". How...

How to remove prefixes into files using the Windows command line

windows,command-line,file-rename,batch-rename

I don't think you can do this with a single line, you'll need a basic batch file. @echo off setlocal enabledelayedexpansion for %%a in (x_*.*) do ( set fname=%%a set fname=!fname:~2! ren "%%a" "!fname!" ) ...

Padding filenames with 0s in batch

batch-file,batch-rename

Set EnableDelayedExpansion then reference the variables with !var! instead of %var% Fixed code: @echo off setlocal EnableDelayedExpansion FOR /L %%G IN (1,1,100) DO ( set num=%%G echo !num! set newnum=0000!num! set newnum=!newnum:~-4! echo !newnum! ren "T%%G.bmp" "T!newnum!.bmp" ) pause endlocal ECHO IS OFF will appear if the variable you are...

How to rename files with list of names using command prompt?

batch-file,command-line,rename,file-rename,batch-rename

@echo off setlocal EnableDelayedExpansion rem The list of filenames will be read from redirected Stdin < filenames.txt ( rem Process the files via FOR command for %%a in (*.*) do ( rem Read the next name from the list set /P name= rem Rename the file ren "%%a" "!name!" )...

Shell script to copy and prepend folder name to files from multiple subdirectories

linux,shell,unix,rename,batch-rename

This is a bit tedious but will do: #!/bin/bash parent=/parent newfolder=/newfolder mkdir "$newfolder" for folder in "$parent"/*; do if [[ -d $folder ]]; then foldername="${folder##*/}" for file in "$parent"/"$foldername"/*; do filename="${file##*/}" newfilename="$foldername"_"$filename" cp "$file" "$newfolder"/"$newfilename" done fi done Put the parent path to parent variable and newfolder path to newfolder...

Rename files based on if condition

excel,rename,file-rename,batch-rename

just tested Sub rename_batch() filePath = "C:\tmp\" counter = 0 For Each c In Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp)) If Dir(filePath & c.Value) <> "" Then Name filePath & c.Value As filePath & c.Offset(counter, 1).Value c.Offset(0, 2).Value = c.Value & " > " & c.Offset(counter, 1).Value Else counter = counter -...

Batch resize and rename image files in subdirectories

bash,imagemagick,batch-rename

You can do this: #!/bin/bash find . -name "*A.jpg" | while read f do newname=${f/A.jpg/[email protected]} echo convert "$f" -resize 400x400 "$newname" done Remove the word echo if it looks correct, and run only on files you have backed up. You can also do it in a one-liner, if you really...

Renaming Multiple Files

windows,windows-7,file-rename,batch-rename

Use a batch file @echo off setlocal enableextensions disabledelayedexpansion cd /d "c:\where\thefiles\are" for /f "tokens=1,2,* delims=#" %%a in (' dir /b /a-d *.pdf ^| findstr /r /b /e /i /c:"[^#][^#-]*#[^#][^#]*#..*\.pdf" ') do echo ren "%%a#%%b#%%c" "%%b#%%a#%%c" What this code does is Get the file list: a dir command asking for...

remove date from filename programatically

regex,batch-file,powershell,batch-rename

PowerShell, untested but should work: $filelist = Get-ChildItem C:\folder | Where-Object {$_.Mode -match "a"} ` | Foreach-Object {$_.FullName} foreach ($fullpath in $filelist) { $newpath = $fullpath -replace "_(19|20)[0-9]{6}" Rename-Item -Path $fullpath -NewName $newpath -WhatIf } The _(19|20)[0-9]{6} regular expression matches the following pattern: leading "_" followed by "19" or "20"...

Removing text from the middle of a file name with batch renaming

windows,batch-file,file-rename,batch-rename

Use the for command. @echo off set YourFileName=title.s1e2.mp4 for /f "tokens=1 delims=." %%x in ("%YourFileName%") do (set filename1=%%x) for /f "tokens=3 delims=." %%x in ("%YourFileName%") do (set filename2=%%x) echo %filename1%.%filename2% rename C:\PATH\%YourFileName% %filename1%.%filename2% pause>nul This should remove the "s1eNum" in the middle, then combine the file name and the extension...

Renaming parts of files with Windows Batch

windows,batch-file,batch-rename

@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET "sourcedir=U:\sourcedir" FOR /f "tokens=2delims=[]" %%a IN ( 'dir /b /a-d "%sourcedir%\[*]*[*].mkv" ' ) DO ( SET "newname=%%a" ECHO(REN "%sourcedir%\[*]%%a[*].mkv" "!newname:~1,-1!.mkv" ) GOTO :EOF This should fix your problem. The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are...

Renaming multiple images with .rename and .endswith

python,rename,batch-rename

The Python string method endswith does not do pattern-matching with *, so you're looking for filenames which explicitly include the asterisk character and not finding any. Try using regular expressions to match your filenames and then building your target filename explicitly: import os import re patt = r'RJ_\d\d\d' user_profile =...

Removing specific part of filename in Linux (what's after the second dash) for all files in folder

linux,bash,media,file-rename,batch-rename

Using a bash script: #!/bin/bash shopt -s extglob for A in *.mp3; do B=${A/%-+([[:alnum:]_]).mp3/.mp3} [[ $A != "$B" ]] && mv "$A" "$B" done That would fix your filenames for files that were already downloaded....

Renaming files in Windows using FOR

windows,batch-file,command-line,batch-rename

Here is a one liner that does not require delayed expansion. From the command line: for /f "tokens=1* delims=:" %A in ('dir /b^|findstr /n "^"') do @ren "%B" "renamed%A.ren" Within a batch script @echo off for /f "tokens=1* delims=:" %%A in ('dir /b^|findstr /n "^"') do ren "%%B" "renamed%%A.ren" ...

Windows batch script to copy newest files with partial names

batch-file,backup,batch-rename

@echo off setlocal enableextensions disabledelayedexpansion set "source=%cd%\source" set "target=%cd%\target" for %%a in (DB_Live DB_Test) do ( set "first=1" for /f "delims=" %%b in (' dir /a-d /tw /o-d /b "%source%\%%a_*.bak" ') do if defined first ( set "first=" copy /b /y "%source%\%%~b" "%target%\%%a%%~xb" ) ) For each set of files,...

Rename a string from the middle of another string in a batch loop?

batch-file,batch-rename

The for replaceable parameters can include a list of delimiters to extract only part of the content in the case of file/folder references (see for /?) In your case %%~dpnf.pdf will return the drive, path, and name of the input file, with the string .pdf attached. for /f "delims=" %%f...

Not looping through all the files

windows,batch-file,batch-rename

@echo on setlocal EnableDelayedExpansion set a=10001 for /f "tokens=*" %%i in ('dir /b "C:\TEST\PBM\PAR*"') do ( set var1=%%i SET sub1=!var1:~7,22! ECHO !sub1! ECHO ren "%%i" "ABC!sub1!_!a!.dat" set /a a+=1 ) With delayedexpansion any reference to the value of a variable that is changed within the loop must use the !var!...

How do I remove extension for renaming purposes?

file,batch-file,batch-rename

Try like this : %original:~0,-4%-added.txt ...

Rename files ordered by date

bash,batch-rename

The problem is that you're quoting "$(ls -Atr | grep [.]jpg)" so you're getting just a long string with all the filenames. This would be a better attempt: #!/bin/bash read -p "Digit new name (no spaces and special chars!): " newname echo i=0 if test "$(ls -A | grep [.]jpg)";...