8 Replies - 259 Views - Last Post: 19 July 2019 - 04:34 AM Rate Topic: -----

#1 gussy81   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 179
  • Joined: 24-February 15

script to move files based on first 4 characters

Posted 16 July 2019 - 07:55 AM

hi,

first of all sorry if this question is in the wrong group. but i have a script i found that helps me move 10 or so pdf files and create folders based on the files names. so for example i have a 1999-notes.pdf file and when i run script the folder '1999-Notes' is created and the pdf file is placed inside it. but i was hoping for a solution where the folder is named after the first 4 characters of the filename - in this case 1999

this is the working script i have currently:

@echo off
setlocal enabledelayedexpansion
for %%A in (*.pdf) do (
   echo file found  %%A
   for /f "delims=" %%B in ("%%A") do set fname=%%~nB
   for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
   for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
   echo folder name !folname!
   if not exist "!folname!" (
      echo Folder !folname! does not exist, creating
      md "!folname!"
   ) else (
      echo Folder !folname! exists
   )
   echo Moving file %%A to folder !folname!
   move "%%A" "!folname!"
   )
echo Finished
pause






thanks
G

Is This A Good Question/Topic? 0
  • +

Replies To: script to move files based on first 4 characters

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15160
  • View blog
  • Posts: 60,694
  • Joined: 12-June 08

Re: script to move files based on first 4 characters

Posted 16 July 2019 - 07:57 AM

What language is this?
You neglected to ask a question.
Was This Post Helpful? 0
  • +
  • -

#3 gussy81   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 179
  • Joined: 24-February 15

Re: script to move files based on first 4 characters

Posted 16 July 2019 - 08:00 AM

its just batch script. not really sure the language.

I am hoping to have the code name the folder after the first 4 characters of the filename instead of using all of the filename to create folder name
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15160
  • View blog
  • Posts: 60,694
  • Joined: 12-June 08

Re: script to move files based on first 4 characters

Posted 16 July 2019 - 08:05 AM

Is this a windows machine?
When you say 'batch' is this like qbasic? Powershell? Bash?
Was This Post Helpful? 0
  • +
  • -

#5 gussy81   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 179
  • Joined: 24-February 15

Re: script to move files based on first 4 characters

Posted 16 July 2019 - 08:06 AM

powershell maybe - but as i said im not really sure what language it is

yes its a windows machine

This post has been edited by gussy81: 16 July 2019 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15160
  • View blog
  • Posts: 60,694
  • Joined: 12-June 08

Re: script to move files based on first 4 characters

Posted 16 July 2019 - 08:10 AM

*sigh* Never mind. I'll bypass the questions on _why_ this is in a batch program.

Since you are yoinking stack over flow - I would look at this. It explains 'substring' in windows batch.
https://stackoverflo...in-a-batch-file
Was This Post Helpful? 1
  • +
  • -

#7 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1860
  • View blog
  • Posts: 6,698
  • Joined: 19-February 09

Re: script to move files based on first 4 characters

Posted 18 July 2019 - 07:21 PM

Perhaps a subroutine would help simplify.

@echo off
title Test Notes Batch %time%
@echo Test Notes Batch

for %%f in (*.pdf) do ( call :subroutine "%%f" )

echo finished
goto bye


:subroutine
  echo %1
  set pdf=%1

  echo pdf is %pdf%

  set folder=%pdf:~1,4%
  echo folder is %folder%

  if not exist "%folder%" (
      echo Folder %folder% does not exist, creating
      md "%folder%"
  ) else (
      echo Folder %folder% exists
  )

  echo Moving file %pdf% to folder %folder%
  move %pdf% "%folder%"

  echo ...
goto :EOF


:bye
set pdf=
set folder=
pause
rem echo bye


Was This Post Helpful? 1
  • +
  • -

#8 gussy81   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 179
  • Joined: 24-February 15

Re: script to move files based on first 4 characters

Posted 19 July 2019 - 02:31 AM

thanks guys all sorted!!
Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7463
  • View blog
  • Posts: 15,469
  • Joined: 16-October 07

Re: script to move files based on first 4 characters

Posted 19 July 2019 - 04:34 AM

First, this is an ancient DOS batch file. Well, semi ancient: Windows CMD it looks like. Um, don't do that.

On any Windows machine with an OS version from the last decade, you should prefer PowerShell. Or python, if you want to install it, though PowerShell comes with.

With this is in mind, here's a freebie, in PowerShell, to hopefully dissuade anyone from using DOS again:
$RootDir="test"

ls $(Join-Path $RootDir "*.pdf") | 
    %{
        $fileInfo = $_
        Write-Output "Found file $($fileInfo.Name)"
        $folderName = $fileInfo.BaseName
        # extra rule, knock off first four ( ok, apparently five) chars
        $folderName = $folderName.Substring(5)
        # normalize to $RootDir
        $folderName = Join-Path $RootDir $folderName
        Write-Output "Folder Name $folderName"
        if (Test-Path $folderName) {
            Write-Output "Folder exists: $folderName"
        } else {
            Write-Output "Folder create: $folderName"
            mkdir $folderName | Out-Null
        }
        Write-Output "Move $($fileInfo.Name) $folderName"
        Move-Item $fileInfo $folderName
    }



As an aside, I needed some test data for this, so I wrote a test generator:
begin {
    $RootDir="test"
    function MkEmptyFile([string]$Name) {
        Write-Output $Name
        New-Item -ItemType File -Path $(Join-Path $RootDir "$Name.pdf")
    }
}
process {
    if (Test-Path $RootDir) {
        Remove-Item -Recurse -Path $RootDir
    }
    mkdir $RootDir
    @(
        "1800-notes"
        "1800-more notes"
        "1900-notes"
        "1900-menu"
    ) | %{ MkEmptyFile -Name $_}
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1