Dosya ismi oluşturma tarihi olsun

Programlama ve Script dilleri konusunda bilgi paylaşım alanıdır.
Cevapla
Kullanıcı avatarı
Email Bot
Kilobyte2
Kilobyte2
Mesajlar: 398
Kayıt: 20 Mar 2022, 23:50
Teşekkür etti: 2 kez
Teşekkür edildi: 35 kez

Dosya ismi oluşturma tarihi olsun

Mesaj gönderen Email Bot »

Aşağıdaki linkte paylaştığınız ipucunda Dosya ismi Değiştirme tarihi yerine Oluşturma tarihi olsun istiyorum , mümkünmüdür.
https://www.sordum.net/59821/dosya-ismi ... ilave-edin
Kullanıcı avatarı
velociraptor
Yottabyte4
Yottabyte4
Mesajlar: 54872
Kayıt: 14 Mar 2006, 02:33
cinsiyet: Erkek
Teşekkür etti: 21124 kez
Teşekkür edildi: 12486 kez

Re: Dosya ismi oluşturma tarihi olsun

Mesaj gönderen velociraptor »

Bat kodlamasını çok seyrek kullanırım , bu konuda nette örnek varmıdır bilemiyorum , ilk aramalarda herhangi bir sonuca ulaşamadım. hep değiştirme tarihi ile ilgili örnekler mevcut malesef mesela değiştirme tarihinin vbs li olanı aşağıda
https://stackoverflow.com/questions/162 ... h-vbscript
bunu ancak deneye yanıla bulabiliriz.
Kullanıcı avatarı
keenimo
Byte3
Byte3
Mesajlar: 110
Kayıt: 03 Tem 2022, 23:25
Teşekkür etti: 9 kez
Teşekkür edildi: 83 kez

Re: Dosya ismi oluşturma tarihi olsun

Mesaj gönderen keenimo »

Kod: Tümünü seç

@echo off
setlocal enabledelayedexpansion

REM Check if a file was dragged onto the batch file
if "%~1"=="" (
    echo No file provided. Drag and drop a file onto this batch file.
    pause
    exit /b
)

REM Use PowerShell to get the creation date of the input file
for /f %%A in ('powershell "(Get-Item \"%~1\").CreationTime.ToString('dd_MM_yyyy')"') do (
    set "creationdate=%%A"
)

REM Get the filename and extension of the input file
set "filename=%~n1"
set "extension=%~x1"

REM Rename the input file with the formatted date as a prefix
ren "%~1" "!creationdate!_%filename%%extension%"

echo File renamed to: "!creationdate!_%filename%%extension%"
bu kodu kopyalayıp not defterine yapıştırın. uzantısı .bat olacak şekilde kaydedin. üzerine sürükleyip bıraktığınız dosya ismi önüne oluşturulma tarihini ekleyecektir.
Kullanıcı avatarı
TRWE_2012
Zettabyte1
Zettabyte1
Mesajlar: 15612
Kayıt: 25 Eyl 2013, 13:38
cinsiyet: Erkek
Teşekkür etti: 2710 kez
Teşekkür edildi: 5626 kez

Re: Dosya ismi oluşturma tarihi olsun

Mesaj gönderen TRWE_2012 »

keenimo yazdı: 10 Eyl 2023, 03:52

Kod: Tümünü seç

@echo off
setlocal enabledelayedexpansion

REM Check if a file was dragged onto the batch file
if "%~1"=="" (
    echo No file provided. Drag and drop a file onto this batch file.
    pause
    exit /b
)

REM Use PowerShell to get the creation date of the input file
for /f %%A in ('powershell "(Get-Item \"%~1\").CreationTime.ToString('dd_MM_yyyy')"') do (
    set "creationdate=%%A"
)

REM Get the filename and extension of the input file
set "filename=%~n1"
set "extension=%~x1"

REM Rename the input file with the formatted date as a prefix
ren "%~1" "!creationdate!_%filename%%extension%"

echo File renamed to: "!creationdate!_%filename%%extension%"
bu kodu kopyalayıp not defterine yapıştırın. uzantısı .bat olacak şekilde kaydedin. üzerine sürükleyip bıraktığınız dosya ismi önüne oluşturulma tarihini ekleyecektir.
Geliştirilmiş DEV Versiyon :

Kod: Tümünü seç

@echo off
setlocal enabledelayedexpansion

REM ============================================================
REM  rename_with_date.bat
REM  Renames dropped file(s) by prepending a date prefix.
REM  Supports CreationTime, LastWriteTime, and LastAccessTime.
REM  Usage: drag and drop one or more files onto this script.
REM ============================================================

REM --- Configuration -------------------------------------------
REM DATE_TYPE options: Created | Modified | Accessed
set "DATE_TYPE=Created"

REM DATE_FORMAT options: dd_MM_yyyy | yyyy_MM_dd | MM_dd_yyyy
set "DATE_FORMAT=dd_MM_yyyy"

REM SKIP_IF_PREFIXED: 1 = skip files already prefixed, 0 = rename anyway
set "SKIP_IF_PREFIXED=1"
REM -------------------------------------------------------------

REM Check if any file was provided
if "%~1"=="" (
    echo.
    echo  [ERROR] No file provided.
    echo  Drag and drop one or more files onto this batch file.
    echo.
    goto :end
)

REM Verify PowerShell is available
where powershell >nul 2>&1
if !errorlevel! neq 0 (
    echo.
    echo  [ERROR] PowerShell not found. This script requires PowerShell.
    echo.
    goto :end
)

set "success_count=0"
set "skip_count=0"
set "error_count=0"

REM Loop through all dropped files
:process_loop
if "%~1"=="" goto :summary

set "input_path=%~1"
set "input_dir=%~dp1"
set "filename=%~n1"
set "extension=%~x1"

REM Check if file actually exists
if not exist "!input_path!" (
    echo  [NOT FOUND]  "!input_path!"
    set /a error_count+=1
    shift
    goto :process_loop
)

REM Skip directories
if exist "!input_path!\" (
    echo  [SKIPPED]    "!filename!!extension!" - directories are not supported
    set /a skip_count+=1
    shift
    goto :process_loop
)

REM Optionally skip files that already look prefixed (dd_MM_yyyy_ pattern)
if "!SKIP_IF_PREFIXED!"=="1" (
    echo !filename! | findstr /r "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_" >nul 2>&1
    if !errorlevel! equ 0 (
        echo  [SKIPPED]    "!filename!!extension!" - already has a date prefix
        set /a skip_count+=1
        shift
        goto :process_loop
    )
)

REM Resolve DATE_TYPE to PowerShell property name
set "ps_prop=CreationTime"
if /i "!DATE_TYPE!"=="Modified" set "ps_prop=LastWriteTime"
if /i "!DATE_TYPE!"=="Accessed" set "ps_prop=LastAccessTime"

REM Build the PowerShell format string
set "ps_fmt=!DATE_FORMAT!"
set "ps_fmt=!ps_fmt:dd=dd!"
set "ps_fmt=!ps_fmt:MM=MM!"
set "ps_fmt=!ps_fmt:yyyy=yyyy!"

REM Get the formatted date via PowerShell
for /f "usebackq delims=" %%A in (
    `powershell -NoProfile -Command "(Get-Item -LiteralPath '!input_path!').!ps_prop!.ToString('!ps_fmt!')" 2^>nul`
) do (
    set "dateprefix=%%A"
)

REM Verify the date was retrieved
if "!dateprefix!"=="" (
    echo  [ERROR]      "!filename!!extension!" - could not retrieve file date
    set /a error_count+=1
    shift
    goto :process_loop
)

REM Build the new filename
set "newname=!dateprefix!_!filename!!extension!"
set "newpath=!input_dir!!newname!"

REM Handle name conflicts by appending a counter
set "counter=1"
:conflict_check
if exist "!newpath!" (
    set "newname=!dateprefix!_!filename!_(!counter!)!extension!"
    set "newpath=!input_dir!!newname!"
    set /a counter+=1
    goto :conflict_check
)

REM Perform the rename
ren "!input_path!" "!newname!" >nul 2>&1
if !errorlevel! equ 0 (
    echo  [OK]         "!filename!!extension!"  ->  "!newname!"
    set /a success_count+=1
) else (
    echo  [ERROR]      "!filename!!extension!" - rename failed (file may be in use or access denied)
    set /a error_count+=1
)

shift
goto :process_loop

REM ---------------------------------------------------------------
:summary
echo.
echo  -----------------------------------------------
echo   Done.  Success: !success_count!   Skipped: !skip_count!   Errors: !error_count!
echo  -----------------------------------------------
echo.

:end
echo Press ENTER to exit.
pause >nul
endlocal
Yapılan Geliştirmeler

Çoklu dosya desteği — artık kaç dosya sürüklenirse sürüklensin hepsini döngüyle işliyor.
Üç adet yapılandırma sabiti (dosyanın başında kolayca değiştirilebilir)

Nasıl Kullanacağız?

Temel Kullanım — Sürükle & Bırak
En basit yöntem budur. Yeniden adlandırmak istediğiniz dosyaları doğrudan .bat dosyasının üzerine sürükleyip bırakırsınız.

Kod: Tümünü seç

[ belge.pdf ]  ------> sürükle ------>  [ rename_with_date.bat ]
Sonuç: 28_05_2025_belge.pdf

Birden fazla dosyayı aynı anda seçip sürükleyebilirsiniz.

CMD / PowerShell üzerinden manuel çalıştırma

Kod: Tümünü seç

rename_with_date.bat "C:\Belgeler\rapor.pdf"
Birden fazla dosya için:

Kod: Tümünü seç

rename_with_date.bat "C:\Belgeler\rapor.pdf" "C:\Belgeler\sunum.pptx"
Not : Sağ tık → Birlikte Aç → rename_with_date.bat seçeneğiyle de çalışır.

Yapılandırma Değişkenleri

Dosyayı Notepad ile açıp en üstteki üç satırı istediğinize göre değiştirebilirsiniz:

Kod: Tümünü seç

set "DATE_TYPE=Created"       ← Created / Modified / Accessed
set "DATE_FORMAT=dd_MM_yyyy"  ← dd_MM_yyyy / yyyy_MM_dd / MM_dd_yyyy
set "SKIP_IF_PREFIXED=1"      ← 1 = zaten önekli dosyayı atla
Ekran Çıktısı Örneği

Kod: Tümünü seç

[OK]         rapor.pdf         ->  28_05_2025_rapor.pdf
 [OK]         sunum.pptx        ->  28_05_2025_sunum.pptx
 [SKIPPED]    28_05_2025_log.txt  - already has a date prefix
 [ERROR]      kilitli.docx      - rename failed (file may be in use)
 -----------------------------------------------
  Done.  Success: 2   Skipped: 1   Errors: 1
 -----------------------------------------------
UYARILAR...!!!

1.PowerShell sisteminizde kurulu olmalıdır (Windows 7 ve sonrasında varsayılan olarak gelir). Dosya başka bir program tarafından açıksa yeniden adlandırma başarısız olur; önce kapatın. Betik dizinleri (klasörleri) işlemez, yalnızca dosyalar için çalışır.

2.Çift tıklama — Eğer dosya yolu argüman gerektirmiyorsa (bu betikte gerektiriyor), çift tıklamak yalnızca hata mesajı gösterir ve kapanır. Bu betik için kullanılmaz.

EKRAN GÖRÜNTÜSÜ :
Resim
Kullanıcı avatarı
TRWE_2012
Zettabyte1
Zettabyte1
Mesajlar: 15612
Kayıt: 25 Eyl 2013, 13:38
cinsiyet: Erkek
Teşekkür etti: 2710 kez
Teşekkür edildi: 5626 kez

Betik Güncellendi Sürüm 2.0'a

Mesaj gönderen TRWE_2012 »

Ana Neden : Dosya adlandırma işlemi sırasında "Dosya'nın içeriği "SİLİNİYORDU"" Ve bu bir "BUG" tu...Tarafımdan bu sorun giderildi...(Allah(c.c)'a şükürler olsun)

Ekran Görüntüleri :

TEST ÖNCESİ, TEST DOSYASI
Resim
TEST DOSYASININ BAT DOSYASINA SÜRÜKLENİP BIRAKILMASI
Resim
YENİDEN İSİMLENDİRİLMİŞ TEST DOSYASI İÇERİK KONTROLU
Resim
YENİ KOD İÇERİĞİ :

Kod: Tümünü seç

@echo off
setlocal enabledelayedexpansion

REM ============================================================
REM  rename_with_date.bat  v2.0
REM  Renames dropped file(s) by prepending a date prefix.
REM  Supports CreationTime, LastWriteTime, and LastAccessTime.
REM  Usage: drag and drop one or more files onto this script.
REM
REM  FIXES vs v1.0:
REM    - BUG FIX : echo line contained unescaped '>' operator
REM                which redirected output into the renamed file,
REM                silently wiping its content. Fixed with '^>'.
REM    - BUG FIX : PowerShell output encoding was not forced to
REM                ASCII; BOM characters could corrupt dateprefix.
REM    - CLEANUP : Removed three no-op ps_fmt substitution lines
REM                (dd=dd, MM=MM, yyyy=yyyy) that did nothing.
REM    - IMPROVE : dateprefix is now reset to empty before each
REM                file so a failed PS call never reuses a stale
REM                value from a previous iteration.
REM    - IMPROVE : Single-quote characters in file paths are now
REM                escaped before being passed to PowerShell to
REM                prevent the PS command from breaking.
REM ============================================================

REM --- Configuration -------------------------------------------
REM DATE_TYPE options: Created | Modified | Accessed
set "DATE_TYPE=Created"

REM DATE_FORMAT options: dd_MM_yyyy | yyyy_MM_dd | MM_dd_yyyy
set "DATE_FORMAT=dd_MM_yyyy"

REM SKIP_IF_PREFIXED: 1 = skip files already prefixed, 0 = rename anyway
set "SKIP_IF_PREFIXED=1"
REM -------------------------------------------------------------

REM Check if any file was provided
if "%~1"=="" (
    echo.
    echo  [ERROR] No file provided.
    echo  Drag and drop one or more files onto this batch file.
    echo.
    goto :end
)

REM Verify PowerShell is available
where powershell >nul 2>&1
if !errorlevel! neq 0 (
    echo.
    echo  [ERROR] PowerShell not found. This script requires PowerShell.
    echo.
    goto :end
)

set "success_count=0"
set "skip_count=0"
set "error_count=0"

REM Loop through all dropped files
:process_loop
if "%~1"=="" goto :summary

set "input_path=%~1"
set "input_dir=%~dp1"
set "filename=%~n1"
set "extension=%~x1"

REM Check if file actually exists
if not exist "!input_path!" (
    echo  [NOT FOUND]  "!input_path!"
    set /a error_count+=1
    shift
    goto :process_loop
)

REM Skip directories
if exist "!input_path!\" (
    echo  [SKIPPED]    "!filename!!extension!" - directories are not supported
    set /a skip_count+=1
    shift
    goto :process_loop
)

REM Optionally skip files that already look prefixed (dd_MM_yyyy_ pattern)
if "!SKIP_IF_PREFIXED!"=="1" (
    echo !filename! | findstr /r "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_" >nul 2>&1
    if !errorlevel! equ 0 (
        echo  [SKIPPED]    "!filename!!extension!" - already has a date prefix
        set /a skip_count+=1
        shift
        goto :process_loop
    )
)

REM Resolve DATE_TYPE to PowerShell property name
set "ps_prop=CreationTime"
if /i "!DATE_TYPE!"=="Modified" set "ps_prop=LastWriteTime"
if /i "!DATE_TYPE!"=="Accessed" set "ps_prop=LastAccessTime"

REM Escape single quotes in path for PowerShell string literal
set "ps_path=!input_path:'=''!"

REM Reset dateprefix before each file to avoid stale values
set "dateprefix="

REM Get the formatted date via PowerShell
REM  - OutputEncoding forced to ASCII to prevent BOM corruption
REM  - Single quotes in path are escaped above
for /f "usebackq delims=" %%A in (
    `powershell -NoProfile -Command "$OutputEncoding=[System.Text.Encoding]::ASCII; (Get-Item -LiteralPath '!ps_path!').!ps_prop!.ToString('!DATE_FORMAT!')" 2^>nul`
) do (
    set "dateprefix=%%A"
)

REM Verify the date was retrieved
if "!dateprefix!"=="" (
    echo  [ERROR]      "!filename!!extension!" - could not retrieve file date
    set /a error_count+=1
    shift
    goto :process_loop
)

REM Build the new filename
set "newname=!dateprefix!_!filename!!extension!"
set "newpath=!input_dir!!newname!"

REM Handle name conflicts by appending a counter
set "counter=1"
:conflict_check
if exist "!newpath!" (
    set "newname=!dateprefix!_!filename!_(!counter!)!extension!"
    set "newpath=!input_dir!!newname!"
    set /a counter+=1
    goto :conflict_check
)

REM Perform the rename
ren "!input_path!" "!newname!" >nul 2>&1
if !errorlevel! equ 0 (
    REM FIX: '>' in '->' was interpreted as a redirect operator in v1.0.
    REM      Escaped with '^>' so CMD treats it as a literal character.
    echo  [OK]         "!filename!!extension!"  -^>  "!newname!"
    set /a success_count+=1
) else (
    echo  [ERROR]      "!filename!!extension!" - rename failed (file may be in use or access denied)
    set /a error_count+=1
)

shift
goto :process_loop

REM ---------------------------------------------------------------
:summary
echo.
echo  -----------------------------------------------
echo   Done.  Success: !success_count!   Skipped: !skip_count!   Errors: !error_count!
echo  -----------------------------------------------
echo.

:end
echo Press ENTER to exit.
pause >nul
endlocal
Güle güle kullanın....! :-)
Cevapla

“Programlama ve Script dilleri” sayfasına dön